/**
 *	@author: Kathleen Go
 *	date created: March 30, 2006
 *	@ver: 1.3
 * 	modified: July 28, 2006
 */

package text.learning;

import java.util.Vector;
import java.util.Hashtable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.StringTokenizer;

import text.ia.TExtAligner;
import text.obj.AlignedChunk;
import text.obj.AlignedSentence;
import text.obj.AlignedTemplate;
import text.obj.TExtChunk;
import text.obj.TExtSentence;
import text.obj.TExtTemplate;
import text.util.DBConnector;
import text.util.TExtLearnerUtility;
import text.util.TExtUtility;

public class TExtTemplateRefiner{
	
	private boolean bSourceIsFilipino;
	private TExtLearnerUtility tlu;
	private Vector vTRTrace;
	private Vector vTRContent;
	private Vector vTRTemplates;
	private Vector vTRChunks;
	private ChunkRefiner cr;
		
	public TExtTemplateRefiner(boolean bSourceIsFilipino1, TExtLearnerUtility tlu) {

		bSourceIsFilipino = bSourceIsFilipino1;
		this.tlu = tlu;
		cr = new ChunkRefiner(bSourceIsFilipino, tlu);
		vTRTrace = new Vector(1);
	}

	/**	This starts the refinement of Template pairs using the given sentence pair (alCurrent).
	 * 
	 *	Returns the a primitive boolean to indicate if there are refined template pairs.
	 *
	 *	@param alCurrent a {@link AlignedSentence} representing a sentence pair that will be
	 *		used to refine existing templates in the Templates DB
	 *	@return the primitive boolean that indicates if there are refined template pairs:
	 *		true - there is at least one template pair that is refined by alCurrent
	 *		false - there are no template pairs refined by alCurrent
	 */
	public boolean startRefinement(AlignedSentence alCurrent){
		boolean isSuccessful = false;
		
		Vector vMatchingTemplates = findMatchingTemplates(alCurrent);
		
		if(vMatchingTemplates.size() != 0)
			isSuccessful = deriveTemplate(alCurrent, vMatchingTemplates);

		return isSuccessful;
	}
	
	public Vector getTRTrace(){
		return vTRTrace;
	}
	
	/**	Retrive from the Templates DB Template pairs having similar tokens with alTemp.
	 *	Returns the Vector containing the Template Pairs and their compound identifiers.
	 */
	protected Vector findMatchingTemplates(AlignedSentence alTemp) {
		Vector vMatchingTemplates = new Vector(1);
		
		String strStmt = new String();
		ResultSet rsTemplates;
		
		TExtSentence senSource = alTemp.getSource();
		TExtSentence senTarget = alTemp.getTarget();
		int nTargetWordSize = senTarget.getWords().size();
		
		String strSourceTable;
		String strTargetTable;
		String strSCompID;
		String strTCompID;
		
		if(bSourceIsFilipino) {
			strSourceTable = "templateFilipino";
			strTargetTable = "templateEnglish";
			strTCompID = "compIDEnglish";
			strSCompID = "compIDFilipino";
		}
		else {
			strSourceTable = "templateEnglish";
			strTargetTable = "templateFilipino";
			strSCompID = "compIDEnglish";
			strTCompID = "compIDFilipino";
		}
			
		strStmt = "Select * From Templates Where " + strSourceTable;
		
		Vector vTemp = senSource.getWords();
		int nLimitS = vTemp.size();
		String strPunc = (String)vTemp.get(vTemp.size()-1);
		if(tlu.isPunctuation(strPunc))
			nLimitS--;
		
		Vector vAlReady = new Vector(5);
		for(int a=0; a<nLimitS; a++) {
			String strWord = (String)vTemp.get(a);
			String strSearch;
			
			if(vAlReady.contains(strWord.toLowerCase()))
				continue;
			vAlReady.add(strWord.toLowerCase());
			
			if(a == 0)
				strSearch = strWord.replace("'","''") + " %";
			else if(a == nLimitS - 1)
				strSearch = "% " + strWord.replace("'","''");
			else
				strSearch = "% " + strWord.replace("'","''") + " %";
			
			rsTemplates = DBConnector.select(strStmt + " Like '" +
				strSearch +"'");
			
			strSearch = null;
			
			try {
				while(rsTemplates.next()) {
					boolean bMatchFound = false;
					String strTargetT = rsTemplates.getString(strTargetTable);
					String strSourceT = rsTemplates.getString(strSourceTable);
					String strTargetID = rsTemplates.getString(strTCompID);
					String strSourceID = rsTemplates.getString(strSCompID);
					
					for(int b=0; !bMatchFound && b<nTargetWordSize; b++) {
						String strCheck = (String)senTarget.getWords().get(b);
						if(strTargetT.toLowerCase().contains(strCheck.toLowerCase()))
							bMatchFound = true;
					}
					if(bMatchFound) {
						boolean bCanInclude = true;
						int nStartIndex = 0;
						int nIndex;
						do{
							nIndex = vMatchingTemplates.indexOf(strSourceT, nStartIndex);
							if(nIndex != -1){
								if(nIndex+3 < vMatchingTemplates.size()
									&& vMatchingTemplates.get(nIndex+1).toString().equals(strSourceID)
									&& vMatchingTemplates.get(nIndex+2).toString().equals(strTargetT)
									&& vMatchingTemplates.get(nIndex+3).toString().equals(strTargetID)){
										
										bCanInclude = false;
										break;
								}
								else if(nIndex+3 >= vMatchingTemplates.size()){
									bCanInclude = true;
									break;
								}
								else
									nStartIndex = nStartIndex++;
							}
							else
								bCanInclude = true;
						}while(nIndex != -1);
						
						if(bCanInclude){
							vMatchingTemplates.add(strSourceT);
							vMatchingTemplates.add(strSourceID);
							vMatchingTemplates.add(strTargetT);
							vMatchingTemplates.add(strTargetID);
						}
					}
				}
				
				rsTemplates.close();
			} catch(SQLException sqle) {
				System.err.println("Template Result Set error: "+sqle.getMessage());
			}
		}
//
		strStmt = null;
		rsTemplates = null;
		vAlReady = null;
//
		return vMatchingTemplates;
	}
	
	/**	Given the Sentence pair (alTemp) and the Template pairs having common tokens with
	 *		alTemp, this initiates the refinement for every pairing of alTemp with each of
	 *		the Template pairs.
	 */
	protected boolean deriveTemplate(AlignedSentence alTemp, Vector vMatchingTemplates){
		TExtTemplate tTempS;
		TExtTemplate tTempT;
		char cPunc;
		int nCount = 0;

		if(vMatchingTemplates.size()%2 == 1)
			return false;
		
		for(Iterator it = vMatchingTemplates.iterator(); it.hasNext(); ) {
			tTempS = tlu.adjustTemplateCompound(new TExtTemplate((String)it.next(), (String)it.next()));
			tTempT = tlu.adjustTemplateCompound(new TExtTemplate((String)it.next(), (String)it.next()));

			AlignedTemplate alTemplate = new AlignedTemplate(tTempS, tTempT);
			Vector vDerived = deriveTemplate(alTemplate, alTemp);
			if(vDerived != null) {
				vTRTrace.add("TR");
				vTRContent = new Vector(1);
				vTRContent.add(alTemp);
				vTRTemplates = new Vector(1);
				vTRChunks = new Vector(1);
				updateDB((AlignedTemplate)vDerived.get(0), (AlignedTemplate)vDerived.get(1), 
					(Vector)vDerived.get(2), (Vector)vDerived.get(3), 
					((Boolean)vDerived.get(4)).booleanValue(), (Hashtable)vDerived.get(5),
					((Boolean)vDerived.get(6)).booleanValue());
				vTRContent.add(vTRTemplates);
				vTRContent.add(vTRChunks);
				vTRTrace.add(vTRContent);
				
				nCount++;
			}
			
			tTempS = null;
			tTempT = null;
			vDerived = null;
		}

		if(nCount == 0)
			return false;
		return true;
	}

//SAME
	/**	This compares the tokens of the given sentence (senTemp) and the candidate
	 *		template (temMatch) to get similar tokens between senTemp and temMatch.
	 */
	protected Vector getSimilarTokens(TExtSentence senTemp, TExtTemplate temMatch){
		boolean hasMatch = false;
		String strMatch;
		String strComp;
		Vector vMatches = new Vector(1);
		
		int c = 0;
		
		int nSenTempSize = senTemp.getWords().size();
		int nTemMatchSize = temMatch.size();
		
		for(int d=0; d<nSenTempSize; d++) {
			strComp = (String)senTemp.getWords().get(d);
			hasMatch = false;
			for(int b=c; !hasMatch && b<nTemMatchSize; b++) {
				strMatch = (temMatch.get(b)).toString();
				if(strComp.compareToIgnoreCase(strMatch) == 0){
					hasMatch = true;
					c = b;
					do {
						vMatches.add(strComp);
						d++; c++;
						if(d>=nSenTempSize || c>=nTemMatchSize)
							break;
						strComp = (String)senTemp.getWords().get(d);
						strMatch = (temMatch.get(c)).toString();
					} while(strComp.compareToIgnoreCase(strMatch) == 0);
					c++;
				}
			}
		}
		return vMatches;
	}
//SAME EXCEPT FOR "RETURN"
	/**	Checks whether the Match Sequence (sequence of Similarities and Differences) of the
	 *		given sentence (senTemp) and the given template (temMatch) are the same
	 *		by using the Vector of similar tokens between them.
	 */
	protected Vector checkPattern(TExtSentence senTemp, TExtTemplate temMatch, Vector vMatches){
		Vector vTemplateTempS = new Vector();
		boolean lastWasChunk = false;
		String strReturn = "true";
		int h = 0;
		int nMatchCtr = 0;
		
		int nTempChunkCtr1 = 0;
		int nTempChunkCtr2 = 0;
		
		int nSenTempSize = senTemp.getWords().size();
		int nMatchesSize = vMatches.size();
		
		for(int f=0; f<nSenTempSize; f++)
			if(nMatchCtr < nMatchesSize && (vMatches.get(nMatchCtr)).toString().equalsIgnoreCase(senTemp.getWords().get(f).toString())){
				System.out.print("String");
				vTemplateTempS.add(new String());
				lastWasChunk = false;
				nMatchCtr++;
			}
			else {
				if(!lastWasChunk) {
					System.out.print("Chunk");
					nTempChunkCtr1++;
					vTemplateTempS.add(new TExtChunk());
				}
				lastWasChunk = true;
			}
			
//		System.out.println();
		nMatchCtr = 0;
		lastWasChunk = false;
		int nTemMatchSize = temMatch.size();
		for(int g=0; g<nTemMatchSize; g++){
			if(nMatchCtr < nMatchesSize && (vMatches.get(nMatchCtr)).toString().equalsIgnoreCase(temMatch.get(g).toString())){
				System.out.print("String");
				if(lastWasChunk)
					h++;
				if(h == vTemplateTempS.size()){
					strReturn = "false";
					break;
				}
				if(vTemplateTempS.get(h) instanceof TExtChunk)
					strReturn = "false";
				lastWasChunk = false;
				h++;
				nMatchCtr++;
			} else {
				if(!lastWasChunk){
					System.out.print("Chunk");
					nTempChunkCtr2++;
				}
				if(h == vTemplateTempS.size()){
					strReturn = "false";
					break;
				}
				if(vTemplateTempS.get(h) instanceof String){
					strReturn = "false";
					System.out.print("NOT MATCH!!!");					
				}
				lastWasChunk = true;
			}
		}
		if(nTempChunkCtr2 != nTempChunkCtr1)
			strReturn = "false";
//		System.out.println();

		vTemplateTempS.add(0, strReturn);
		return vTemplateTempS;
	}

//SAME
	/**	The Differences (sequence of tokens not common in the given sentence and
	 *		template) or chunks that will be learned are constructed and the initial
	 *		new template is also constructed as a StringBuffer wherein the
	 *		Differences are enclosed in square brackets.
	 */
	protected Vector constructChunks(Vector vTokens, Vector vMatches, String strCompoundIdentifier, int nRetNextChunk){
		Vector vChunks = new Vector();
		StringBuffer sbTemp = new StringBuffer();
		Object token;
		boolean isInHashTable = false;
		TExtChunk cTemp;
		Object otemp;
		Vector vTempChunk = null;
		String strNextChunk = null;
		int nChunkTracker;
		boolean lastWasChunk = false;
		int nMatchCtr = 0;		
		
		int nLBound = 0;
		int nUBound;
		Vector vRemoved = new Vector(3);
		Vector vRemoved2;
		int nStart = -1;
		nChunkTracker = 0;
		
		int nMatchesSize = vMatches.size();

		int nTokens = vTokens.size();
		for(int l=0; l<nTokens; l++){
			token = vTokens.get(l);
			if(nMatchCtr < nMatchesSize && (vMatches.get(nMatchCtr)).toString().equalsIgnoreCase(token.toString())){
				if(lastWasChunk){
					vRemoved.add(nStart + "-" + (nLBound-1));
					sbTemp.deleteCharAt(sbTemp.length()-1);
					sbTemp.append("]");
					sbTemp.append(" ");
					if(vTempChunk.size() == 1){
						otemp = vTempChunk.get(0);
						if(otemp instanceof TExtChunk){
							vChunks.add(otemp);
							nChunkTracker--;				
						}
						else{
							cTemp = new TExtChunk(tlu.toStringChunk(vTempChunk), "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
							cTemp = tlu.adjustChunkCompound(cTemp);
							vChunks.addElement(cTemp);
						}
					}
					else{
						cTemp = new TExtChunk(tlu.toStringChunk(vTempChunk), "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
						cTemp = tlu.adjustChunkCompound(cTemp);
						vChunks.addElement(cTemp);
					}
				}
				nMatchCtr++;
				sbTemp.append(token);
				sbTemp.append(" ");
				if(token instanceof String && token.toString().contains(" "))
					nLBound += (new StringTokenizer(token.toString())).countTokens()-1;	
				lastWasChunk = false;
			} else{
				if(!lastWasChunk){
					nStart = nLBound;
					vTempChunk = new Vector(3);
					sbTemp.append("[");
					strNextChunk = ""+(nRetNextChunk+nChunkTracker);
					nChunkTracker++;
				}
				sbTemp.append(token);
				if(!(strCompoundIdentifier.equals("none")) && 
					token instanceof String && token.toString().contains(" ")){
						nUBound = nLBound + (new StringTokenizer(token.toString())).countTokens()-1;
						nLBound = nUBound;
				}
				sbTemp.append(" ");
				vTempChunk.addElement(token);
				lastWasChunk = true;
			}
			nLBound++;
		}
		if(lastWasChunk){
			vRemoved.add(nStart + "-" + (nLBound-1));
			sbTemp.deleteCharAt(sbTemp.length()-1);
			String sbTempTemp = new String(sbTemp);
			sbTemp.append("]");
			sbTemp.append(" ");
			if(vTempChunk.size() == 1){
				otemp = vTempChunk.get(0);
				if(otemp instanceof TExtChunk){
					vChunks.add(otemp);
					nChunkTracker--;					
				}
				else{
					cTemp = new TExtChunk(tlu.toStringChunk(vTempChunk), "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
					cTemp = tlu.adjustChunkCompound(cTemp);
					vChunks.addElement(cTemp);
				}
			}
			else{
				cTemp = new TExtChunk(tlu.toStringChunk(vTempChunk), "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
				cTemp = tlu.adjustChunkCompound(cTemp);
				vChunks.addElement(cTemp);
			}
		}
		vChunks.add(sbTemp);
		
		if(!(strCompoundIdentifier.equals("none"))){
			vRemoved2 = tlu.editCompID(strCompoundIdentifier, vRemoved);
			strCompoundIdentifier = tlu.adjustCompIDVal(tlu.removeCompID(strCompoundIdentifier, vRemoved2), vRemoved);
			vChunks.add(strCompoundIdentifier);		
		}
		return vChunks;
	}

//SAME	
	/**	Constructs the new template by replacing the Differences (identified chunks) with their
	 *		corresponding domains.
	 */
	protected StringBuffer constructTemplate(String strTemp, Vector vAlChunk2){
		Vector vAlChunk = (Vector)vAlChunk2.clone();
		StringBuffer sbChunk = new StringBuffer();
		boolean bChunkScan = false;
		String strChunkTemp;
		int nOpen = 0;
		int nClose = 0;
		
		StringBuffer sbTemplate = new StringBuffer();
		
		int nTempLength = strTemp.length();
		for(int p=0; p<nTempLength; p++) {
			if(!bChunkScan)
				sbTemplate.append(strTemp.charAt(p));
			else
				sbChunk.append(strTemp.charAt(p));
			if(strTemp.charAt(p) == '[')
				nOpen++;
			if(strTemp.charAt(p)=='[' && !bChunkScan) {
				sbChunk = new StringBuffer();
				sbChunk.append(strTemp.charAt(p));
				bChunkScan = true;
			} else if(strTemp.charAt(p)==']') {
				nClose ++;
				if(nOpen == nClose){
					StringBuffer sbOpen = new StringBuffer();
					StringBuffer sbClose = new StringBuffer();
					for(int nCtr = 0; nCtr < nOpen; nCtr++){
						sbOpen.append("[");
						sbClose.append("]");
					}
					nOpen = 0;
					nClose = 0;
					for(int q=0; q<vAlChunk.size(); q++) {
						TExtChunk cCheck = (TExtChunk)vAlChunk.get(q);
						strChunkTemp = cCheck.toString();
						if(!(strChunkTemp.startsWith(sbOpen.toString()) && strChunkTemp.endsWith(sbClose.toString())))
							strChunkTemp = "[" + strChunkTemp + "]";
						if(strChunkTemp.equals(sbChunk.toString())) {
							vAlChunk.remove(q);
							q--;
							
							if(cCheck.domain().startsWith("[") && cCheck.domain().endsWith("]"))
								sbTemplate.append(cCheck.domain().substring(1, cCheck.domain().length()-1));
							else
								sbTemplate.append(cCheck.domain());
							break;
						}
					}
					sbTemplate.append(strTemp.charAt(p));
					bChunkScan = false;
				}
			}
		}
//
		vAlChunk = null;
		sbChunk = null;
//
		return sbTemplate;	
	}

	/**	Adjust the StringBuffer representation of the refinement (new template)
	 *		such that chunks like "[[1.1]]" will be converted to "[1.1]".
	 */
	protected StringBuffer adjustTemplateChunks(StringBuffer templateTemp){
		int nStart;
		int nEnd;
		String strMiddle = null;
		
		nStart = templateTemp.indexOf("[[");
		nEnd = templateTemp.indexOf("]]", nStart);
		while(nStart != -1 && nEnd != -1){
			strMiddle = templateTemp.substring(nStart+1, nEnd+1);
			if(!(strMiddle.contains(" "))){
				templateTemp = templateTemp.replace(nStart, nEnd+2, strMiddle);
			}
			nStart = templateTemp.indexOf("[[", nEnd+1);
			nEnd = templateTemp.indexOf("]]", nStart);
		}
		strMiddle = null;
		return templateTemp;
	}

	/**	This adjusts the domain of the aligned Source and Target Chunks such that their
	 *		domains must be the same.
	 */
	protected Vector adjustDomain(Vector vSource, Vector vTarget){
		TExtChunk tempS;
		TExtChunk tempT;
		Vector vSourceFinal = new Vector(vSource.size());
		Vector vTargetFinal = new Vector(vTarget.size());
		int nLargest = 0;
		Vector vSourceDomain = new Vector(vSource.size());
		Vector vTargetDomain = new Vector(vTarget.size());		
		
		int nSourceCount = 0;
		

		for(Iterator s = vSource.iterator(); s.hasNext(); )
			if(((TExtChunk)s.next()).size() == 0)
				nSourceCount++;
		
		if(nSourceCount == vSource.size()){
			try{
				nLargest = Integer.parseInt(tlu.returnNextChunk()) - 1;
			}
			catch(Exception e){
				
			}
		}
		else{
			for(Iterator s = vSource.iterator(); s.hasNext(); ){
				TExtChunk chunkTemp = (TExtChunk)s.next();
				if(chunkTemp.size() > 0){
					try{
						nLargest = Integer.parseInt((chunkTemp.domain()).substring(1, (chunkTemp.domain()).length()-1));
					}
					catch(Exception e){
					}
				}
			}
		}

		for(Iterator s = vSource.iterator(), t = vTarget.iterator(); s.hasNext() && t.hasNext(); ){
			tempS = (TExtChunk)s.next();
			tempT = (TExtChunk)t.next();
			if(tempS.size() > 0 && tempT.size() == 0 ||
				tempS.size() > 0 && tempT.size() > 0){
					tempT.setDomain(tempS.domain());
			}
			else if(tempS.size() == 0 && tempT.size() == 0){
				String strSDom = tempS.domain();
				String[] strSChunkDom = (strSDom.substring(1, strSDom.length()-1)).split("/");
				boolean bHasWhole = false;
				int nDomCount = strSChunkDom.length;
				for(int i = 0; i < nDomCount && !bHasWhole; i++)
					if(!(strSChunkDom[i].contains(".")))
						bHasWhole = true;
				if(!bHasWhole){
					nLargest++;
					tempS.setDomain(new String("[" + nLargest + "/" + strSDom.substring(1, strSDom.length()-1) + "]"));
				}
				tempT.setDomain(tempS.domain());
			}
			else if(tempS.size() == 0 && tempT.size() > 0){
				nLargest++;
				tempS.setDomain(new String("[" + nLargest + "]"));
				tempT.setDomain(tempS.domain());
			}
			if(tempS.size() == 0)
				vSourceDomain.add(tempS.domain());
			if(tempT.size() == 0)
				vTargetDomain.add(tempT.domain());
			vSourceFinal.add(tempS);
			vTargetFinal.add(tempT);
		}

//
		vSource = null;
		vTarget = null;
//
		Vector vFinal = new Vector(4);
		vFinal.add(vSourceFinal);
		vFinal.add(vTargetFinal);
		vFinal.add(vSourceDomain);
		vFinal.add(vTargetDomain);
		return vFinal;
	}	

	/**	Given a Vector of chunks, this function converts these chunks to String form
	 *		together with its domain and compound identifier and place it in a single
	 *		Vector.
	 *	This returns the Vector containing the String forms of the chunks.
	 */
	protected Vector toStringVector(Vector vChunks){
		Vector vAlChunkString = new Vector(3);
		TExtChunk transfer;
		for(Iterator it = vChunks.iterator(); it.hasNext(); ){
			transfer = (TExtChunk)it.next();
			vAlChunkString.add(transfer.toString());
			vAlChunkString.add(transfer.domain());
			vAlChunkString.add(transfer.getCompoundIdentifier());
		}
		return vAlChunkString;
	}
	
	/**	After aligment of chunks, the chunks would not be in their original order.
	 *	This function simply gets the relation (based on indices) of the new order
	 *		of chunks (vNewOrdeR) to the original order of chunks (vOriginalOrder)
	 */
	protected Vector arrangeOrder(Vector vOriginalOrder, Vector vNewOrder, boolean isForBuffer){
		int nIndex2;
		TExtChunk tempChunk;
		Vector nTargetDomain = new Vector(vOriginalOrder.size());
		
		for(Iterator it = vNewOrder.iterator(); it.hasNext(); ){
			TExtChunk tempChunk2 = (TExtChunk)it.next();
			nIndex2 = -1;
			for(Iterator it2 = vOriginalOrder.iterator(); it2.hasNext(); ){
				tempChunk = (TExtChunk)it2.next();
				if(isForBuffer && tempChunk.size() == 0)
					nIndex2++;
				else if(!isForBuffer)
					nIndex2++;
				if(isForBuffer && tempChunk.size() == 0 && 
					tempChunk.toString().equals(tempChunk2.toString()) && 
					tempChunk.domain().equals(tempChunk2.domain()) && 
					!(nTargetDomain.contains(new Integer(nIndex2)))) {
						
					nTargetDomain.add(new Integer(nIndex2));
					break;
				}
				if(!isForBuffer && tempChunk.toString().equals(tempChunk2.toString()) && 
					tempChunk.domain().equals(tempChunk2.domain()) && 
					!(nTargetDomain.contains(new Integer(nIndex2)))) {
					
					nTargetDomain.add(new Integer(nIndex2));
					break;
				}
			}
		}
		return nTargetDomain;
	}
	
	/**	This function modifies the StringBuffer representation of the new template
	 *		specifically the chunks that will be retained (chunks containing domains
	 *		only e.g. [1]) after the chunks, specifically their domains, have been adjusted.
	 */
	protected StringBuffer mapDomain(Vector vDomain, StringBuffer sbTemp, Vector nDomainOrder){
		int nDomainSize = vDomain.size();
		int nChunkPlace;
		String strTempChunk;
		StringTokenizer st = new StringTokenizer(sbTemp.toString());
		String[] strTemplateBuffer = new String[st.countTokens()];
		int nCtr=0;
		while(st.hasMoreTokens()){
			strTemplateBuffer[nCtr] = (String)st.nextToken();
			nCtr++;
		}
//		
		st = null;
//
		int nOpen = 0;
		int nClose = 0;
		
		for(int k = 0; k < nDomainSize; k++){
			nChunkPlace = ((Integer)nDomainOrder.get(k)).intValue();
			int nChunk = -1;
			for(int m = 0; m < strTemplateBuffer.length; m++){
				strTempChunk = strTemplateBuffer[m];
				int nOpenTemp = 0;
				int nCloseTemp = 0;
				if(strTempChunk.contains("[") || strTempChunk.contains("]")){
					for(int n = 0; n < strTempChunk.length(); n++)
						if(strTempChunk.charAt(n) == '['){
							nOpen++;
							nOpenTemp++;
						}
						else if(strTempChunk.charAt(n) == ']'){
							nClose++;
							nCloseTemp++;
						}
				}
				if(nOpen == nClose && nOpenTemp == nCloseTemp && 
					strTempChunk.startsWith("[") && strTempChunk.endsWith("]")){
					
					String strMiddle = strTempChunk.substring(1, strTempChunk.length()-1);
					boolean isNumber = true;
					int nMiddle = strMiddle.length();
					for(int n = 0; n < nMiddle; n++)
						if(!(Character.isDigit(strMiddle.charAt(n))) && strMiddle.charAt(n) != '.' && strMiddle.charAt(n) != '/')
							isNumber = false;
					if(isNumber){
						nChunk++;
						if(nChunk == nChunkPlace){
							strTemplateBuffer[m] = (String)vDomain.get(k);
							break;						
						}
					}
				}
			}
		}
		sbTemp = new StringBuffer();
		int nBuffer = strTemplateBuffer.length;
		for(int k = 0; k < nBuffer; k++){
			if(k > 0)
				sbTemp.append(" ");
			sbTemp.append(strTemplateBuffer[k]);
		}
//
		strTemplateBuffer = null;
//
		return sbTemp;
	}

	/**	Given a Vector of String chunks, they are converted to TExtChunk objects.  The template
	 *		is used to to get the domains of the chunks.
	 */
	protected Vector getOriginalChunks(TExtTemplate template, Vector vAlChunkString){
		Vector vTemplateChunk = new Vector(2);
		Object oContent;
		Vector vAlChunkFinal = new Vector(5);
		String strChunkString;
		String strChunkDom;
		String strCompID;
		int nRunner = 0;

		for(Iterator it = template.iterator(); it.hasNext(); ){
			oContent = it.next();
			if(oContent instanceof TExtChunk)
				vTemplateChunk.add(((TExtChunk)oContent).domain());
		}
//
		oContent = null;
//
		for(Iterator it = vAlChunkString.iterator(); it.hasNext(); ){
			strChunkString = (String)it.next();
			strChunkDom = (String)it.next();
			TExtChunk newChunk = new TExtChunk();
			strCompID = (String)it.next();
			if(strChunkString.compareTo(strChunkDom) == 0)
				vAlChunkFinal.add(new TExtChunk(strChunkDom + "->" + (String)vTemplateChunk.get(nRunner)));
			else
				vAlChunkFinal.add(new TExtChunk(strChunkString, (String)vTemplateChunk.get(nRunner), strCompID));
			
			nRunner++;
			if(nRunner == vTemplateChunk.size())
				nRunner = 0;
		}
		return vAlChunkFinal;
	}
	
	/**	This function gets the tokens of the chunks to be aligned and passes them to Unit
	 *		Alignment.
	 */
	protected Vector getAlignmentInChunks(Vector vSourceChunks1, Vector vTargetChunks1){
		Vector vSourceTokens = new Vector();
		Vector vTargetTokens = new Vector();
		String strDummy = null;
		Hashtable hTemplate;
		Vector vUpdatedSChunks = new Vector(vSourceChunks1.size());
		Vector vUpdatedTChunks = new Vector(vTargetChunks1.size());
		
		TExtAligner aligner;
		
		if(bSourceIsFilipino)
			aligner = new TExtAligner('e');
		else
			aligner = new TExtAligner('f');
			
		for(Iterator it1 = vSourceChunks1.iterator(); it1.hasNext(); ){
			TExtChunk cSource = (TExtChunk)it1.next();
			if(cSource.size() == 0)
				vSourceTokens.add(cSource.domain());
			else
				for(Iterator it2 = cSource.iterator(); it2.hasNext(); )
					vSourceTokens.add(it2.next().toString());
			vSourceTokens.add(strDummy);
		}
		
		for(Iterator it1 = vTargetChunks1.iterator(); it1.hasNext(); ){
			TExtChunk cTarget = (TExtChunk)it1.next();
			if(cTarget.size() == 0)
				vTargetTokens.add(cTarget.domain());
			else
				for(Iterator it2 = cTarget.iterator(); it2.hasNext(); )
					vTargetTokens.add(it2.next().toString());
			vTargetTokens.add(strDummy);
		}

		hTemplate = aligner.align(vSourceTokens, vTargetTokens, false);

		Vector vFinalTokens = aligner.getUdpatedTokens();
//
		aligner = null;
//
		vSourceTokens = (Vector)vFinalTokens.get(0);
		vTargetTokens = (Vector)vFinalTokens.get(1);
		
		int nRun = 0;
		Vector vTokens = new Vector();
		for(Iterator it1 = vSourceTokens.iterator(); it1.hasNext(); ){
			String strFromFinal = (String)it1.next();
			
			if(strFromFinal == null){
				TExtChunk cSource;
				if(vTokens.size() == 1 && vTokens.get(0).toString().startsWith("[") && vTokens.get(0).toString().endsWith("]")){
					cSource = new TExtChunk();
					vTokens.clear();
					cSource.setChunk(vTokens, ((TExtChunk)vSourceChunks1.get(nRun)).domain(), "none");
				}
				else
					cSource = tlu.adjustChunkCompound(new TExtChunk(tlu.toStringChunk(vTokens), ((TExtChunk)vSourceChunks1.get(nRun)).domain(), tlu.makeCompoundIdentifier(vTokens)));
				vTokens = new Vector();
				nRun++;
				vUpdatedSChunks.add(cSource);
			}
			else
				vTokens.add(strFromFinal);
		}

		nRun = 0;
		vTokens = new Vector();
		for(Iterator it1 = vTargetTokens.iterator(); it1.hasNext(); ){
			String strFromFinal = (String)it1.next();
			
			if(strFromFinal == null){
				TExtChunk cTarget;
				if(vTokens.size() == 1 && vTokens.get(0).toString().startsWith("[") && vTokens.get(0).toString().endsWith("]")){
					cTarget = new TExtChunk();
					vTokens.clear();
					cTarget.setChunk(vTokens, ((TExtChunk)vTargetChunks1.get(nRun)).domain(), "none");
				}
				else
					cTarget = tlu.adjustChunkCompound(new TExtChunk(tlu.toStringChunk(vTokens), ((TExtChunk)vTargetChunks1.get(nRun)).domain(), tlu.makeCompoundIdentifier(vTokens)));
				vTokens = new Vector();
				nRun++;
				vUpdatedTChunks.add(cTarget);
			}
			else
				vTokens.add(strFromFinal);
		}

//
		vSourceChunks1 = null;
		vTargetChunks1 = null;
//
		Vector vResults = new Vector();
		vResults.add(vUpdatedSChunks);
		vResults.add(vUpdatedTChunks);
		vResults.add(hTemplate);
		return vResults;
	}
	

	/**	This is the main function for Template Refinement.
	 *	This calls all the functions that processes the given and performs the refinement
	 *		process.
	 */
	private Vector deriveTemplate(AlignedTemplate alTemplate, AlignedSentence alSentence){
		TExtTemplate tSource = alTemplate.getSource();
		TExtTemplate tTarget = alTemplate.getTarget();
		
		TExtSentence senTempSTemp = alSentence.getSource(), senTempTTemp = alSentence.getTarget();
		Vector vMatchesS;
		Vector vMatchesT;
		Vector vResult;
		
		TExtTemplate oldS = new TExtTemplate(tSource.toString(), tSource.getCompoundIdentifier());
		TExtTemplate oldT = new TExtTemplate(tTarget.toString(), tTarget.getCompoundIdentifier());
		
		oldS = tlu.adjustTemplateCompound(oldS);
		oldT = tlu.adjustTemplateCompound(oldT);
		
	 	Vector vTempTokens = (Vector)((Vector)senTempSTemp.getWords()).clone();
	 	vTempTokens.remove(vTempTokens.size()-1);
	 	TExtSentence senTempS = new TExtSentence(vTempTokens, senTempSTemp.toString());
	 	
	 	vTempTokens = (Vector)((Vector)senTempTTemp.getWords()).clone();
	 	vTempTokens.remove(vTempTokens.size()-1);
	 	TExtSentence senTempT = new TExtSentence(vTempTokens, senTempTTemp.toString());
//
		senTempSTemp = null;
		senTempTTemp = null;
		vTempTokens = null;
//
		
		
		vResult = getSimilarTokens(senTempS, tSource);
	
		vMatchesS = (Vector)vResult.clone();
//FOR COMMON WORDS		
		if(tlu.computeSpecificity(vMatchesS, true) == 0)
			return null;
		
		
//END FOR COMMON WORDS
		
		
		if(senTempS.getWords().size() == vMatchesS.size() || vMatchesS.size() == 0)
			return null;

//		System.out.println("Checkpoint 0.5");
		
		vResult = getSimilarTokens(senTempT, tTarget);

		vMatchesT = (Vector)vResult.clone();

//FOR COMMON WORDS		
		if(tlu.computeSpecificity(vMatchesT, false) == 0)
			return null;
//END FOR COMMON WORDS
		if(senTempT.getWords().size() == vMatchesT.size() || vMatchesT.size() == 0)
			return null;

		/* At this point, there must be at least one unit match for both
		 * source and target language parts of the aligned sentence and template pair.
		 */
//		System.out.println("Checkpoint 1");
				
		/* Checking to see whether the aligned sentence and the template pair share
		 * the same valid template pattern.
		 */
		Vector vConfirmS = checkPattern(senTempS, tSource, vMatchesS);
		Vector vConfirmT = checkPattern(senTempT, tTarget, vMatchesT);
		if(((vConfirmS.get(0)).toString()).compareTo("false") == 0)
			return null;

		if(((vConfirmT.get(0)).toString()).compareTo("false") == 0)
			return null;

//
		vConfirmS = null;
		vConfirmT = null;
//
		
//		System.out.println("Checkpoint 2");

//FOR VALIDITY OF CONSTANTS		
		if(!tlu.isValidSimilarities((Vector)vMatchesS.clone(), (Vector)vMatchesT.clone(), alSentence.getAlignment()))
			return null;
//END FOR VALIDITY OF CONSTANTS
			
		/* Constructing the template String and forming the corresponding
		 * TExtChunks along the way.
		 */
	
		int nRetNextChunk = Integer.parseInt(tlu.returnNextChunk());
		vResult = constructChunks(senTempS.getWords(), vMatchesS, "none", nRetNextChunk);
		StringBuffer sbTempS1 = (StringBuffer)vResult.get(vResult.size()-1);
		vResult.remove(vResult.size()-1);
		Vector vSourceChunks1 = (Vector)vResult.clone();
		
		String newSCompID = new String("none");
		if(!(tSource.getCompoundIdentifier().equals("none"))){
			newSCompID = new String(tSource.getCompoundIdentifier());
			vResult = constructChunks(tSource, vMatchesS, newSCompID, nRetNextChunk);
			newSCompID = (String)vResult.get(vResult.size()-1);
			vResult.remove(vResult.size()-1);
		}
		else
			vResult = constructChunks(tSource, vMatchesS, newSCompID, nRetNextChunk);
		StringBuffer sbTempS2 = (StringBuffer)vResult.get(vResult.size()-1);
		vResult.remove(vResult.size()-1);
		Vector vSourceChunks2 = (Vector)vResult.clone();
		
		vResult = constructChunks(senTempT.getWords(), vMatchesT, "none", nRetNextChunk);
		StringBuffer sbTempT1 = (StringBuffer)vResult.get(vResult.size()-1);
		vResult.remove(vResult.size()-1);
		Vector vTargetChunks1 = (Vector)vResult.clone();
		
		String newTCompID = new String("none");
		if(!(tTarget.getCompoundIdentifier().equals("none"))){
			newTCompID = new String(tTarget.getCompoundIdentifier());
			vResult = constructChunks(tTarget, vMatchesT, newTCompID, nRetNextChunk);
			newTCompID = (String)vResult.get(vResult.size()-1);
			vResult.remove(vResult.size()-1);
		}
		else
			vResult = constructChunks(tTarget, vMatchesT, newTCompID, nRetNextChunk);
		StringBuffer sbTempT2 = (StringBuffer)vResult.get(vResult.size()-1);
		vResult.remove(vResult.size()-1);
		Vector vTargetChunks2 = (Vector)vResult.clone();

		vResult = null;
		vMatchesS = null;
		vMatchesT = null;
//
		/* At this point, a template String and element TExtChunks must have
		 * been formed.
		 */
		
		Vector vFcnResults = getAlignmentInChunks((Vector)vSourceChunks2.clone(), (Vector)vTargetChunks2.clone());
		vSourceChunks2 = (Vector)vFcnResults.get(0);
		vTargetChunks2 = (Vector)vFcnResults.get(1);
		Hashtable hTemplate = (Hashtable)vFcnResults.get(2);
//
		vFcnResults = null;
//

		TExtChunkSplitter txtChunkSplit1, txtChunkSplit2;
		txtChunkSplit1 = new TExtChunkSplitter(bSourceIsFilipino, tlu);
		txtChunkSplit2 = new TExtChunkSplitter(bSourceIsFilipino, tlu);
		Vector vChosen1 = null;
		Vector vChosen2 = null;
		Vector vTempS1 = null;
		Vector vTempS2 = null;
		Vector vTempT1 = null;
		Vector vTempT2 = null;
		if(txtChunkSplit1.alignChunks(vSourceChunks1, vTargetChunks1, alSentence.getAlignment(), 1) &&
			txtChunkSplit2.alignChunks(vSourceChunks2, vTargetChunks2, hTemplate, 2)){
			
			Vector vPossRes1 = txtChunkSplit1.getFinalResult();
			Vector vPossRes2 = txtChunkSplit2.getFinalResult();
			Vector vChosenRes = tlu.chooseAmongAlSplit(vPossRes1, vPossRes2);
			
			if(vChosenRes != null){
				vChosen1 = (Vector)vChosenRes.get(0);
				vChosen2 = (Vector)vChosenRes.get(1);
//
				vPossRes1 = null;
				vPossRes2 = null;
				vChosenRes = null;
//				
				
				vSourceChunks1 = (Vector)vChosen1.get(1);
				vTargetChunks1 = (Vector)vChosen1.get(2);
				vTempS1 = (Vector)vChosen1.get(3);
				vTempT1 = (Vector)vChosen1.get(4);
				Vector vDummy1 = tlu.adjustChunkDiv(sbTempS1.toString(), (Vector)vSourceChunks1.clone(), "none");
				Vector vDummy2 = tlu.adjustChunkDiv(sbTempT1.toString(), (Vector)vTargetChunks1.clone(), "none");

				if(vDummy1 == null || vDummy2 == null)
					return null;

				sbTempS1 = (StringBuffer)vDummy1.remove(0);
				sbTempT1 = (StringBuffer)vDummy2.remove(0);
				
				vSourceChunks2 = (Vector)vChosen2.get(1);
				vTargetChunks2 = (Vector)vChosen2.get(2);
				vTempS2 = (Vector)vChosen2.get(3);
				vTempT2 = (Vector)vChosen2.get(4);
				vDummy1 = tlu.adjustChunkDiv(sbTempS2.toString(), (Vector)vSourceChunks2.clone(), newSCompID);
				vDummy2 = tlu.adjustChunkDiv(sbTempT2.toString(), (Vector)vTargetChunks2.clone(), newTCompID);

				if(vDummy1 == null || vDummy2 == null)
					return null;

				if(vDummy1.size() == 2){
					sbTempS2 = (StringBuffer)vDummy1.remove(1);
					newSCompID = new String((String)vDummy1.remove(0));
				}
				else
					sbTempS2 = (StringBuffer)vDummy1.remove(0);
					
				if(vDummy2.size() == 2){
					sbTempT2 = (StringBuffer)vDummy2.remove(1);
					newTCompID = new String((String)vDummy2.remove(0));
				}
				else
					sbTempT2 = (StringBuffer)vDummy2.remove(0);
					
				vDummy1 = null;
				vDummy2 = null;
			}
			else
				return null;
		}
		else
			return null;
//
		txtChunkSplit1 = null;
		txtChunkSplit2 = null;
//		
		Vector vAlChunkSString = new Vector(2);
		Vector vAlChunkTString = new Vector(2);
		
		vAlChunkSString.addAll(toStringVector(vSourceChunks1));
		vAlChunkSString.addAll(toStringVector(vSourceChunks2));
		vAlChunkTString.addAll(toStringVector(vTargetChunks1));
		vAlChunkTString.addAll(toStringVector(vTargetChunks2));

		Vector vAlChunkS1 = new Vector(vSourceChunks1.size()), vAlChunkT1 = new Vector(vTargetChunks1.size());
		Vector vAlChunkS2 = new Vector(vSourceChunks2.size()), vAlChunkT2 = new Vector(vTargetChunks2.size());

		for(Iterator it = vSourceChunks1.iterator(); it.hasNext(); ){
			TExtChunk outer = (TExtChunk)it.next();
			for(int k = 0; k < vTempS1.size(); k++){
				TExtChunk inner = (TExtChunk)vTempS1.get(k);
				if(outer.equals(inner)){
					vAlChunkS1.add(inner);
					TExtChunk cAlTarget = (TExtChunk)vTempT1.get(k);
					cAlTarget.setDomain(inner.domain());
					vAlChunkT1.add(cAlTarget);
					vTempS1.remove(k);
					vTempT1.remove(k);
					break;
				}
			}
		}

		for(Iterator it = vSourceChunks2.iterator(); it.hasNext(); ){
			TExtChunk outer = (TExtChunk)it.next();
			for(int k = 0; k < vTempS2.size(); k++){
				TExtChunk inner = (TExtChunk)vTempS2.get(k);
				if(outer.equals(inner)){
					vAlChunkS2.add(inner);
					TExtChunk cAlTarget = (TExtChunk)vTempT2.get(k);
					cAlTarget.setDomain(inner.domain());
					vAlChunkT2.add(cAlTarget);
					vTempS2.remove(k);
					vTempT2.remove(k);
					break;
				}
			}
		}

		Vector nSourceDomain = arrangeOrder(vSourceChunks2, vAlChunkS2, true);
		Vector nTargetDomain = arrangeOrder(vTargetChunks2, vAlChunkT2, true);
		
		Vector nSourceDomainTemp = arrangeOrder(vSourceChunks2, vAlChunkS2, false);
		
//		System.out.println("Checkpoint 4");
		
		sbTempS1 = adjustTemplateChunks(sbTempS1);
		sbTempS2 = adjustTemplateChunks(sbTempS2);
		sbTempT1 = adjustTemplateChunks(sbTempT1);
		sbTempT2 = adjustTemplateChunks(sbTempT2);
		
		Vector vDomainResult = adjustDomain((Vector)vAlChunkS2.clone(), (Vector)vAlChunkT2.clone());
		
		vAlChunkS2 = (Vector)vDomainResult.get(0);
		vAlChunkT2 = (Vector)vDomainResult.get(1);
		Vector vSourceDomain = (Vector)vDomainResult.get(2);
		Vector vTargetDomain = (Vector)vDomainResult.get(3);

//
		vDomainResult = null;
//

		sbTempS2 = mapDomain(vSourceDomain, sbTempS2, nSourceDomain);
		sbTempT2 = mapDomain(vTargetDomain, sbTempT2, nTargetDomain);

		Vector vReArrangeS = (Vector)vAlChunkS2.clone();
		Vector vReArrangeT = (Vector)vAlChunkT2.clone();

		for(int i = 0; i < nSourceDomainTemp.size(); i++){
			int nPosition = ((Integer)nSourceDomainTemp.get(i)).intValue();
			TExtChunk nNewS = (TExtChunk)vReArrangeS.get(i);
			vAlChunkS2.setElementAt(nNewS, nPosition);
			TExtChunk nNewT = (TExtChunk)vReArrangeT.get(i);
			vAlChunkT2.setElementAt(nNewT, nPosition);
		}

//
	vReArrangeS = null;
	vReArrangeT = null;
//
		
		int nSize = vAlChunkS2.size();
		TExtChunk tempTemp;
		TExtChunk tempInput;
		for(int k = 0; k < nSize; k++){
			tempTemp = (TExtChunk)vAlChunkS2.get(k);
			tempInput = (TExtChunk)vAlChunkS1.get(k);
			tempInput.setDomain(tempTemp.domain());
			vAlChunkS1.setElementAt(tempInput, k);

			tempTemp = (TExtChunk)vAlChunkT2.get(k);
			tempInput = (TExtChunk)vAlChunkT1.get(k);
			tempInput.setDomain(tempTemp.domain());
			vAlChunkT1.setElementAt(tempInput, k);
		}

		StringBuffer sbTemplateS, sbTemplateT;

		sbTemplateS = constructTemplate(sbTempS2.toString(), vAlChunkS2);
		
		sbTemplateT = constructTemplate(sbTempT2.toString(), vAlChunkT2);
		
		TExtTemplate tSource2 = new TExtTemplate(sbTemplateS.toString(), newSCompID, 'r');
		TExtTemplate tTarget2 = new TExtTemplate(sbTemplateT.toString(), newTCompID, 'r');
		tSource2 = tlu.adjustTemplateCompound(tSource2);
		tTarget2 = tlu.adjustTemplateCompound(tTarget2);

	/*	JOptionPane.showMessageDialog(null, "TR");
		JOptionPane.showMessageDialog(null, "tSource2-->" + tSource2);
		JOptionPane.showMessageDialog(null, "tTarget2-->" + tTarget2);
	*/	
		Vector vAlChunkSRef = getOriginalChunks(tSource2, vAlChunkSString);
		Vector vAlChunkTRef = getOriginalChunks(tTarget2, vAlChunkTString);
		
		int nRefS = vAlChunkSRef.size();
		for(int i = nRefS/2; i < nRefS; i++){
			TExtChunk ref = (TExtChunk)vAlChunkSRef.get(i);
			TExtChunk changeS = (TExtChunk)vAlChunkS2.get(i-vAlChunkSRef.size()/2);
			if(changeS.size() == 0){
				changeS.setDomain(ref.domain());
				vAlChunkS2.setElementAt(changeS, i-vAlChunkSRef.size()/2);
			}

			TExtChunk changeT = (TExtChunk)vAlChunkT2.get(i-vAlChunkSRef.size()/2);
			if(changeT.size() == 0){
				if(ref.domain().contains("->"))
					changeT.setDomain(ref.domain());
				else{
					int nRefT = vAlChunkTRef.size();
					for(int j = nRefT/2; j < nRefT; j++){
						TExtChunk targetRef = (TExtChunk)vAlChunkTRef.get(j);
						if(targetRef.size() == 0 && targetRef.domain().contains("->"+ref.domain()))
							changeT.setDomain(targetRef.domain());		
					}
				}
				vAlChunkT2.setElementAt(changeT, i-vAlChunkSRef.size()/2);
			}
		}
		
		Vector vAlChunkSFinal = new Vector(2);
		Vector vAlChunkTFinal = new Vector(2);
		vAlChunkSFinal.addAll(vAlChunkS1);
		vAlChunkSFinal.addAll(vAlChunkS2);
		vAlChunkTFinal.addAll(vAlChunkT1);
		vAlChunkTFinal.addAll(vAlChunkT2);
//
		vAlChunkS1 = null;
		vAlChunkS2 = null;
		vAlChunkT1 = null;
		vAlChunkT2 = null;
//
	
		for(int i = 0; i < vAlChunkSFinal.size(); i++){
			TExtChunk cChunkS = (TExtChunk)vAlChunkSFinal.get(i);
			String strDomain = cChunkS.domain();
			if(strDomain.contains("->")){
				String[] oldNew = strDomain.split("->");
				if(oldNew[0].compareTo(oldNew[1]) == 0){
					cChunkS.setDomain(oldNew[0]);
					vAlChunkSFinal.setElementAt(cChunkS, i);
				}
			}
		}

		for(int i = 0; i < vAlChunkTFinal.size(); i++){
			TExtChunk cChunkT = (TExtChunk)vAlChunkTFinal.get(i);
			String strDomain = cChunkT.domain();
			if(strDomain.contains("->")){
				String[] oldNew = strDomain.split("->");
				if(oldNew[0].compareTo(oldNew[1]) == 0){
					cChunkT.setDomain(oldNew[0]);
					vAlChunkTFinal.setElementAt(cChunkT, i);
				}
			}
		}
		
		/* At this point, TExtTemplates and TExtChunks must have already been formulated.
		 */
		Vector vNewRefinement = new Vector(7);
		AlignedTemplate alNewTemplate = new AlignedTemplate(tSource2, tTarget2);
		AlignedTemplate alOldTemplate = new AlignedTemplate(oldS, oldT);
		vNewRefinement.add(alNewTemplate);
		vNewRefinement.add(alOldTemplate);
		vNewRefinement.add(vAlChunkSFinal.clone());
		vNewRefinement.add(vAlChunkTFinal.clone());
		vNewRefinement.add(new Boolean(!bSourceIsFilipino));
		vNewRefinement.add(tlu.combineHash(alSentence.getAlignment(), hTemplate));
		vNewRefinement.add(new Boolean(true));
		return vNewRefinement;
	}		

//MIN START - updateDBWithRefinement
	/**
	 * Updates the database by: inserting the new, refined TExtTemplates in
	 *	vSourceRefinements and vTargetRefinements into and removing the old 
	 *	TExtTemplate objects in vOldSourceBases from the Templates Database;
	 *	and, inserting the new TExtChunks in vChunkAlignments into the Chunks
	 *	Database.
	 * This works in tandem with TExtLearner.refineTemplate(AS,V) and
	 *	especially with TExtLearner[Kat].checkForRefinement(T,T,V,V)
	 *
	 * @param Vector vSourceRefinements vector of refined TExtTemplate objects
	 *	in the source language
	 * @param Vector vTargetRefinements vector of refined TExtTemplate objects
	 *	in the target language
	 * @param Vector vOldSourceBases vector of old TExtTemplate objects to
	 *	remove from the Templates Database
	 * @param Vector vChunkAlignments vector of new TExtChunk objects to store
	 *	into the Chunks Database
	 * @param boolean bSourceIsEnglish flag indicating whether the source
	 *	objects are in English
	 *
	 * @see TExtLearner.updateDatabaseWithRefinement(V,V,V,V,b)
	 *
	 * @note Check the comments in TExtLearner.refineTemplate(AS,V) just
	 *	after Checkpoint D.
	 *
	 *	not tested
	 */
	public void updateDB(AlignedTemplate atRefinement,
		AlignedTemplate atOldTemplate,
		Vector vSourceChunks,
		Vector vTargetChunks,
		boolean bSourceIsEnglish,
		Hashtable hAlignedWords,
		boolean bIsFromRefineTemplate) {

		vTRContent.add(atOldTemplate);
		
		TExtTemplate tSourceRefinement = (TExtTemplate)atRefinement.getSource();
		TExtTemplate tTargetRefinement = (TExtTemplate)atRefinement.getTarget();
		TExtTemplate tOldSrcTemplate = null;
		TExtTemplate tOldTargetTemplate = null;
		String strSource, strTarget, strSComp, strTComp;
		boolean bCheck = true,
			bExactDuplicate = false;
		ResultSet rsDuplicate = null;
		AlignedTemplate atNewRefinement = null;
				
		DBConnector.setAutoCommit(false);
		
		if(bSourceIsEnglish) {
			strSource = "English";
			strTarget = "Filipino";
		} else {
			strSource = "Filipino";
			strTarget = "English";			
		}

		//check for duplicate templates with different vars
		//if there is a duplicate -> ignore all processes...
		//checks: for an existing refined template in the DB
		rsDuplicate = DBConnector.select("SELECT templateID FROM Templates WHERE "
			+ "template" + strSource + " = '" 
			+ tSourceRefinement.toString().replace("'","''") + "'"
			+ " AND template" + strTarget + " = '" 
			+ tTargetRefinement.toString().replace("'","''") + "'"
			+ " AND compID" + strSource + " = '" 
			+ tSourceRefinement.getCompoundIdentifier() + "'"
			+ " AND compID" + strTarget + " = '" 
			+ tTargetRefinement.getCompoundIdentifier() + "'");

		try {
			if(rsDuplicate.next()) {
				bExactDuplicate = true;
			}
		} catch(SQLException sqle){
			sqle.printStackTrace();

			System.out.println("There was an error while inserting the refined" 
			+" templates in the database.");

			bCheck = false;
		}

		if(!bExactDuplicate) {
			atNewRefinement = checkForDuplicateTempWithDiffVar(tSourceRefinement,
										tTargetRefinement, strSource, strTarget);
										
			if(atNewRefinement != null) {
				tSourceRefinement = atNewRefinement.getSource();
				tTargetRefinement = atNewRefinement.getTarget();
			}
		}

		if(bCheck) {
			if(bIsFromRefineTemplate) {
				tOldSrcTemplate = (TExtTemplate)atOldTemplate.getSource();
				tOldTargetTemplate = (TExtTemplate)atOldTemplate.getTarget();
		
//				System.out.println("**************************************************");
//				System.out.println("Delete the old template from the database.");
		
				bCheck = DBConnector.update("DELETE FROM Templates WHERE "
					+ "template" + strSource + " = '" 
					+ tOldSrcTemplate.toString().replace("'","''") + "'"
					+ " AND template" + strTarget + " = '" 
					+ tOldTargetTemplate.toString().replace("'","''") + "'"
					+ " AND compID" + strSource + " = '" 
					+ tOldSrcTemplate.getCompoundIdentifier() + "'"
					+ " AND compID" + strTarget + " = '" 
					+ tOldTargetTemplate.getCompoundIdentifier() + "'");
		
				if(!bCheck)
					System.out.println("There is no such template to be remove, or, "
						+ "such a template has already been removed.");
				else
					System.out.println("Template successfully removed.");
			}
	
//			System.out.println("**************************************************");
//			System.out.println("Update the template database with the refinements.");

			if(atNewRefinement == null || bExactDuplicate) {
				int nCnstCntS = 0, nCnstCntT = 0;
				
				for(Iterator it = tSourceRefinement.iterator(); it.hasNext(); ) {
					Object o = it.next();
					if(o instanceof String){
//						System.out.println(o);
						nCnstCntS++;
					}
					o = null;
				}
				for(Iterator it = tTargetRefinement.iterator(); it.hasNext(); ) {
					Object o = it.next();
					if(o instanceof String){
//						System.out.println(o);
						nCnstCntT++;
					}
					o = null;
				}
	
				bCheck = DBConnector.update("INSERT INTO Templates"
					+ " (template" + strSource + ", template" + strTarget 
					+ ", compID" + strSource + ", compID" + strTarget
					+ ", constCnt" + strSource.substring(0,3) 
					+ ", constCnt" + strTarget.substring(0,3)
					+ ") VALUES ('" 
					+ tSourceRefinement.toString().replace("'","''") + "', '" 
					+ tTargetRefinement.toString().replace("'","''") + "', '" 
					+ tSourceRefinement.getCompoundIdentifier() + "', '" 
					+ tTargetRefinement.getCompoundIdentifier() + "', '"
					+ nCnstCntS + "', '"
					+ nCnstCntT + "')");
				
				if(vTRTemplates.size() > 0) {
					vTRTemplates.removeElementAt(vTRTemplates.size()-1);
				}
				vTRTemplates.add(atNewRefinement);
			}
		}

		if(!bCheck)
			System.out.println("There was an error while adding the refined templates.");
		else
			System.out.println("Refined templates successfully added.");
			

		if(bCheck) {
	
			/**
			 *	for adding chunks in the DB
			 *
			 *	assumptions:
			 *		- the given chunks are aligned (meaning there is an equal number of
			 *		chunks in the source and in the target vector of chunks)
			 *		- there is always a whole number in the domain of a given chunk
			 *		- for each given chunk in the vector of source and target,
			 *		the source and target domain is equal
			 *		- domains have square brackets and slashes
			 */

	 		if(vSourceChunks.size() == vTargetChunks.size()) {
	 			//assumes: cSource.domain() == cTarget.domain()
	 			TExtChunk cSource = null;
	 			TExtChunk cTarget = null;
	 			String strSrcDomain = null;
	 			String strTarDomain = null;
	 			
		 		for(int i = 0; i < vSourceChunks.size(); i++) {
		 			cSource = (TExtChunk) vSourceChunks.get(i);
		 			cTarget = (TExtChunk) vTargetChunks.get(i);
		 			
		 			strSrcDomain = cSource.domain();
		 			strTarDomain = cTarget.domain();
	
		 			/**
		 			 *	if either of the given chunk (source/target) is domain only
		 			 *	
		 			 *	assumptions:
		 			 *		- if the chunk contains only a domain and -> exists 
		 			 *		then the chunk is to be added
		 			 *		- else it is just ignored
		 			 */
		 			if(cSource.size() <= 0 || cTarget.size() <= 0) {
		 				
		 				String[] arrStrSrcDomains = null;
		 				String[] arrStrTarDomains = null;
		 				String strSNewDom = null;
		 				String strTNewDom = null;
		 				
		 				if(strSrcDomain.contains("->") && strTarDomain.contains("->")){
		 					arrStrSrcDomains = strSrcDomain.split("->");
							arrStrTarDomains = strTarDomain.split("->");
							strSNewDom = checkDomain(arrStrSrcDomains[1],
								vSourceChunks, vTargetChunks, cSource, cTarget);
							strTNewDom = checkDomain(arrStrTarDomains[1],
								vSourceChunks, vTargetChunks, cSource, cTarget);

		 					//check if there's a similar X.Y in the dom
		 					//ex. [1.1]->[2/1.1]; [1.1]->[4]
		 					//if there is then no need to add it
		 					if(!isChunkADomAndInDomain(arrStrSrcDomains[0],
		 						arrStrSrcDomains[1]) &&
		 						!isChunkADomAndInDomain(arrStrTarDomains[0],
		 						arrStrTarDomains[1])) {
								//check if checkDomain changed in the whole number part
								//&& if X.Y is already in the DB: assume: X.Y already exists
				 				if(getWholeNoDomain(arrStrSrcDomains[1]) == null &&
				 					!arrStrSrcDomains[1].substring(0,
				 					arrStrSrcDomains[1].indexOf(".")).equalsIgnoreCase(strSNewDom.substring(0,
				 					strSNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tSourceRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, arrStrSrcDomains[1],
				 						strSNewDom, strSource, strTarget, true);
								}
								
								//check if checkDomain changed in the whole number part
				 				if(getWholeNoDomain(arrStrTarDomains[1]) == null &&
				 					!arrStrTarDomains[1].substring(0,
				 					arrStrTarDomains[1].indexOf(".")).equalsIgnoreCase(strTNewDom.substring(0,
				 					strTNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tTargetRefinement = modOrigin(tSourceRefinement, 
				 						tTargetRefinement, arrStrTarDomains[1],
				 						strTNewDom, strSource, strTarget, false);
								}
				 				
			 					addChunkToDB(new TExtChunk(arrStrSrcDomains[0],
	 								strSNewDom, cSource.getCompoundIdentifier()),
	 								new TExtChunk(arrStrTarDomains[0],
	 								strTNewDom, cTarget.getCompoundIdentifier()),
	 								strSource, strTarget, tSourceRefinement,
	 								tTargetRefinement, vSourceChunks, vTargetChunks);
		 					}
		 				}else if(strSrcDomain.contains("->")) {
//			 				System.out.println("<---ARROW2-->");
							arrStrSrcDomains = strSrcDomain.split("->");
							strSNewDom = checkDomain(arrStrSrcDomains[1],
		 						vSourceChunks, vTargetChunks, cSource, cTarget);
		 						
		 					//check if there's a similar X.Y in the dom
		 					//ex. [1.1]->[2/1.1]
		 					//if there is then no need to add it
		 					if(!isChunkADomAndInDomain(arrStrSrcDomains[0],
		 						arrStrSrcDomains[1])) {

								//check if checkDomain changed in the whole number part
				 				if(getWholeNoDomain(arrStrSrcDomains[1]) == null &&
				 					!arrStrSrcDomains[1].substring(0,
				 					arrStrSrcDomains[1].indexOf(".")).equalsIgnoreCase(strSNewDom.substring(0,
				 					strSNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tSourceRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, arrStrSrcDomains[1],
				 						strSNewDom, strSource, strTarget, true);
									tTargetRefinement = modOrigin(tSourceRefinement,
										tTargetRefinement, arrStrSrcDomains[1],
										strSNewDom, strSource, strTarget, false);
								}
			 					
				 				addChunkToDB(new TExtChunk(arrStrSrcDomains[0],
	 								strSNewDom,
	 								cSource.getCompoundIdentifier()), 
	 								new TExtChunk(cTarget.toString(),
	 								strSNewDom,
	 								cTarget.getCompoundIdentifier()),
	 								strSource, strTarget,
	 								tSourceRefinement, tTargetRefinement,
	 								vSourceChunks, vTargetChunks);
 							}
						}else if(strTarDomain.contains("->")) {
//			 				System.out.println("<---ARROW3-->");
							arrStrTarDomains = strTarDomain.split("->");
							strTNewDom = checkDomain(arrStrTarDomains[1],
								vSourceChunks, vTargetChunks, cSource, cTarget);
								
		 					//check if there's a similar X.Y in the dom
		 					//ex. [1.1]->[2/1.1]
		 					//if there is then no need to add it
		 					if(!isChunkADomAndInDomain(arrStrTarDomains[0],
		 						arrStrTarDomains[1])) {
								//check if checkDomain changed in the whole number part

				 				if(getWholeNoDomain(arrStrTarDomains[1]) == null &&
				 					!arrStrTarDomains[1].substring(0,
				 					arrStrTarDomains[1].indexOf(".")).equalsIgnoreCase(strTNewDom.substring(0,
				 					strTNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tSourceRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, arrStrTarDomains[1],
				 						strTNewDom, strSource, strTarget, true);
				 					tTargetRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, arrStrTarDomains[1],
				 						strTNewDom, strSource, strTarget, false);
								}

								addChunkToDB(new TExtChunk(cSource.toString(),
	 								strTNewDom,
	 								cSource.getCompoundIdentifier()), 
	 								new TExtChunk(arrStrTarDomains[0],
	 								strTNewDom,
	 								cTarget.getCompoundIdentifier()),
	 								strSource, strTarget,
	 								tSourceRefinement, tTargetRefinement,
	 								vSourceChunks, vTargetChunks);
							}
						}
						
		 				arrStrSrcDomains = null;
		 				arrStrTarDomains = null;
		 				strSNewDom = null;
		 				strTNewDom = null;

					}else{
//		 				System.out.println("<---CHUNKS-->");
		 				
						//get all chunks + domain in the DB with domain same as the domain of the Chunk
			 			Vector vChunksDB = getChunksFrDB(strSrcDomain, strSource, strTarget);
//						System.out.println("vChunksDB: "+vChunksDB);
		 					
			 			if(vChunksDB != null) {
							//storage of source TExtChunks in the DB
				 			Vector vSrcChunksFrDB = (Vector) vChunksDB.get(0);
							//storage of target TExtChunks in the DB
				 			Vector vTarChunksFrDB = (Vector) vChunksDB.get(1);
							boolean bMatch = false;

							int nDBIndex = 0;
							
							TExtChunk cSrcChunksFrDB = null;
							TExtChunk cTarChunksFrDB = null;
							boolean bSChunkFrDBisNotXY;
							boolean bTChunkFrDBisNotXY;
							boolean bSIsMatch;
							boolean bTIsMatch;

							//while all chunksDB && no match
							while(nDBIndex < vSrcChunksFrDB.size() && !bMatch) {
								//does the chunks in both source and target 
								//match to the chunks in the DB
								cSrcChunksFrDB = (TExtChunk)vSrcChunksFrDB.get(nDBIndex);
								cTarChunksFrDB = (TExtChunk)vTarChunksFrDB.get(nDBIndex);
								bSChunkFrDBisNotXY = chunkFrDBisNotXY(cSrcChunksFrDB);
								bTChunkFrDBisNotXY = chunkFrDBisNotXY(cTarChunksFrDB);
								bSIsMatch = isMatch(cSrcChunksFrDB, cSource);
								bTIsMatch = isMatch(cTarChunksFrDB, cTarget);
								
								if(bSChunkFrDBisNotXY &&
									bTChunkFrDBisNotXY &&
									bSIsMatch && 
									bTIsMatch) {

//									System.out.println("-->IS MATCH<--");
									//assign the domain of the chunks (S&T) based on the chunks in the DB
									System.out.println("v1694 findChunkDomains() "+ atRefinement);
									System.out.println("v1965 vSourceChunks: "+ vSourceChunks);
									Vector vAOriginAndChunksWithDomains = findChunkDomains((TExtChunk)vSrcChunksFrDB.get(nDBIndex),
												(TExtChunk)vTarChunksFrDB.get(nDBIndex), cSource, cTarget, strSource,
												strTarget, hAlignedWords, vSourceChunks, vTargetChunks);
									
									if(vAOriginAndChunksWithDomains != null) {
										bMatch = true;
										//this calls addChunkToDB()
										bCheck = addAlignedChunksToDB(vAOriginAndChunksWithDomains, strSource, strTarget,
													vSourceChunks, vTargetChunks);
									}//else: if vAChunksWithDomains or there are no subchunks to be added
									
									vAOriginAndChunksWithDomains = null;
								}
								nDBIndex++;
							}
							//no match means new domain
							if(!bMatch){
			 					String strSNewDom = checkDomain(strSrcDomain,
										vSourceChunks, vTargetChunks, cSource, cTarget);
			 					//--->>> error#2
			 					//ensure that there's a chunk learned before adding a domain in the template										
								//check if checkDomain changed in the whole number part
				 				if(getWholeNoDomain(strSrcDomain) == null &&
				 					!strSrcDomain.substring(0,
				 					strSrcDomain.indexOf(".")).equalsIgnoreCase(strSNewDom.substring(0,
				 					strSNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tSourceRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, strSrcDomain, strSNewDom,
				 						strSource, strTarget, true);
				 					tTargetRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, strSrcDomain, strSNewDom,
				 						strSource, strTarget, false);
								}

								bCheck = addChunkToDB(new TExtChunk(cSource.toString(),
											strSNewDom,
											cSource.getCompoundIdentifier()),
											new TExtChunk(cTarget.toString(),
											strSNewDom,
											cTarget.getCompoundIdentifier()),
											strSource, strTarget,
											tSourceRefinement, tTargetRefinement,
											vSourceChunks, vTargetChunks);
					 			//System.out.println("NEW DOMAIN ADDED END - no match");
					 			strSNewDom = null;
							}
							
							vSrcChunksFrDB = null;
							vTarChunksFrDB = null;
							
							cSrcChunksFrDB = null;
							cTarChunksFrDB = null;							
						}
						//else: chunksDB == null
						else {
		 					String strSNewDom = checkDomain(strSrcDomain,
									vSourceChunks, vTargetChunks, cSource, cTarget);
									
							//check if checkDomain changed in the whole number part
			 				if(getWholeNoDomain(strSrcDomain) == null &&
			 					!strSrcDomain.substring(0,
			 					strSrcDomain.indexOf(".")).equalsIgnoreCase(strSNewDom.substring(0,
			 					strSNewDom.indexOf(".")))) {
								//if it did then change the template or the chunk
			 					tSourceRefinement = modOrigin(tSourceRefinement,
			 						tTargetRefinement, strSrcDomain, strSNewDom,
			 						strSource, strTarget, true);
			 					tTargetRefinement = modOrigin(tSourceRefinement,
			 						tTargetRefinement, strSrcDomain, strSNewDom,
			 						strSource, strTarget, false);
							}

							bCheck = addChunkToDB(new TExtChunk(cSource.toString(),
											strSNewDom,
											cSource.getCompoundIdentifier()),
											new TExtChunk(cTarget.toString(),
											strSNewDom,
											cTarget.getCompoundIdentifier()),
											strSource, strTarget,
											tSourceRefinement, tTargetRefinement,
											vSourceChunks, vTargetChunks);
					 		//System.out.println("NEW DOMAIN ADDED END - not in DB");
					 		strSNewDom = null;
						}
					}//end of else with if(cSource.size() <= 0 || cTarget.size() <= 0)

				}//end of for-loop

		 		//generalize parent domain here...
		 		//save the generalization to DB .. or call modifyparentdomain, domains are blank
		 		
		 		modifyParentDomain(tSourceRefinement,tTargetRefinement,"","" );

		 		
	 			cSource = null;
	 			cTarget = null;
	 			strSrcDomain = null;
	 			strTarDomain = null;

				atNewRefinement = new AlignedTemplate(tSourceRefinement, tTargetRefinement);
				if(vTRTemplates.size() > 0) {
					vTRTemplates.removeElementAt(vTRTemplates.size()-1);
				}
				vTRTemplates.add(atNewRefinement);

				//--------------->>>check if all doms of the templates are in the db
				//checkDBDomainExistence(tSourceRefinement, tTargetRefinement, strSource, strTarget);

	 		}else {
	 			System.out.println("Error! Source and Target Chunks are unequal.");
	 		}
		}
		
		if(!bCheck)
			System.out.println("There was an error while adding the chunks.");
		else
			System.out.println("Chunks successfully added.");

		//END
		
		if(!bCheck)
			DBConnector.rollback();
		else
			DBConnector.commit();
		
		DBConnector.setAutoCommit(true);

		//null objects
		tSourceRefinement = null;
		tTargetRefinement = null;
		tOldSrcTemplate = null;
		tOldTargetTemplate = null;
		
		strSource = null;
		strTarget = null;
		strSComp = null;
		strTComp = null;

		rsDuplicate = null;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	protected AlignedTemplate checkForDuplicateTempWithDiffVar(TExtTemplate tSourceRefinement,
		TExtTemplate tTargetRefinement, String strSource, String strTarget) {
		Object oTmp = null,
			oTmpRef = null;
		TExtTemplate tTmp = null;
		Vector vSTemplatesFrDB = new Vector();
		Vector vTTemplatesFrDB = new Vector();
		StringBuffer strbSRegexp = new StringBuffer("^");
		StringBuffer strbTRegexp = new StringBuffer("^");
		ResultSet rsDomain = null;

		//check if the templates have whole no vars
		for(int i = 0; i < tSourceRefinement.size(); i++) {
			oTmp = tSourceRefinement.get(i);
			if(oTmp instanceof TExtChunk) {
				strbSRegexp.append("\\\\[([0-9]+(\\\\.[0-9]+)?/?)+\\\\]");
			}else {
				strbSRegexp.append(oTmp);
			}
			strbSRegexp.append(" ");
		}
		
		for(int i = 0; i < tTargetRefinement.size(); i++) {
			oTmp = tTargetRefinement.get(i);
			if(oTmp instanceof TExtChunk) {
				strbTRegexp.append("\\\\[([0-9]+(\\\\.[0-9]+)?/?)+\\\\]");
			}else {
				strbTRegexp.append(oTmp);
			}
			strbTRegexp.append(" ");
		}

			//select all in the db with same structure
			rsDomain = DBConnector.select("SELECT template"+
			strSource + " , template" + strTarget + " , compID" + strSource +
			" , compID" + strTarget + 
			" FROM Templates"+
			" WHERE template"+strSource+
			" REGEXP '"+strbSRegexp.toString().trim().replace("'","''")+"$"+
			"' AND template"+strTarget+
			" REGEXP '"+strbTRegexp.toString().trim().replace("'","''")+"$"+
			"' AND compID"+strSource+" = '"+tSourceRefinement.getCompoundIdentifier()+
			"' AND compID"+strTarget+" = '"+tTargetRefinement.getCompoundIdentifier()+"'");
	
			try {
				while(rsDomain.next()){
					String strSrc = rsDomain.getString("template" + strSource);
					String strTar = rsDomain.getString("template" + strTarget);
					String strCompIDS = rsDomain.getString("compID" + strSource);
					String strCompIDT = rsDomain.getString("compID" + strTarget);
					vSTemplatesFrDB.add(tlu.adjustTemplateCompound(new TExtTemplate(strSrc, strCompIDS)));
					vTTemplatesFrDB.add(tlu.adjustTemplateCompound(new TExtTemplate(strTar, strCompIDT)));
					
				}
			} catch(SQLException sqle){
				System.out.println("There is an SQL Error!");
				sqle.printStackTrace();
			}
			
			AlignedTemplate atNewRefinement = null;
			TExtTemplate tNewSource = null;
			TExtTemplate tNewTarget = null; 
			StringBuffer strbNewSource = new StringBuffer();
			StringBuffer strbNewTarget = new StringBuffer();
			StringBuffer strbDBSource = new StringBuffer();
			StringBuffer strbDBTarget = new StringBuffer();
			String[] arrStrDBVar;
			String[] arrStrRef;
			Vector vFinVar = new Vector(1);
			boolean bRefIsInDB;
			Vector vAddedDomain = new Vector(1);
			//check the selected db if there is already a template
			//that have whole no vars
			for(int i = 0; i < vSTemplatesFrDB.size(); i++) {
				tTmp = (TExtTemplate)vSTemplatesFrDB.get(i);
				for(int j = 0; j < tTmp.size(); j++) {
					oTmp = tTmp.get(j);
					if(oTmp instanceof TExtChunk) {
						vFinVar.clear();
						oTmpRef = (TExtChunk)tSourceRefinement.get(j);
						
						//check if var in tSourceRef is already in oTmp whole no: no need
						strbNewSource.append("[");
						arrStrDBVar = stripSquareBrackets(oTmp.toString()).split("/");
						arrStrRef = stripSquareBrackets(oTmpRef.toString()).split("/");
						
						for(int k = 0; k < arrStrDBVar.length ; k++) {
							bRefIsInDB = false;
							for(int l = 0; l < arrStrRef.length && !bRefIsInDB; l++) {
								if(!vFinVar.contains(arrStrDBVar[k]) && 
									!isWholePresent(arrStrDBVar[k], arrStrDBVar, arrStrRef) &&
									(arrStrDBVar[k].equalsIgnoreCase(arrStrRef[l])
									|| (arrStrRef[l].contains(".") && 
									arrStrDBVar[k].equalsIgnoreCase(arrStrRef[l].substring(0,
									arrStrRef[l].indexOf(".")))))) 		{
									strbNewSource.append(arrStrDBVar[k]);
									bRefIsInDB = true;
									vFinVar.add(arrStrDBVar[k]);
									if(k < arrStrDBVar.length) {
										strbNewSource.append("/");
									}
								}else if(!vFinVar.contains(arrStrRef[l]) && 
									!isWholePresent(arrStrRef[l], arrStrDBVar, arrStrRef) &&
									arrStrDBVar[k].contains(".") && 
									arrStrRef[l].equalsIgnoreCase(arrStrDBVar[k].substring(0,
									arrStrDBVar[k].indexOf(".")))) {
									strbNewSource.append(arrStrRef[l]);
									bRefIsInDB = true;
									vFinVar.add(arrStrRef[l]);
									if(k < arrStrDBVar.length) {
										strbNewSource.append("/");
									}
								}
							}
							
							if((!vFinVar.contains(arrStrDBVar[k])
								|| (!vFinVar.contains(arrStrDBVar[k]) && arrStrDBVar[k].contains(".")
								&& !vFinVar.contains(arrStrDBVar[k].substring(0,
								arrStrDBVar[k].indexOf(".")))))
								&& !isWholePresent(arrStrDBVar[k], arrStrDBVar, arrStrRef)
								&& !bRefIsInDB) {
								//v added this if
								if(!vFinVar.contains(arrStrDBVar[k])){
									strbNewSource.append(arrStrDBVar[k]);
									vFinVar.add(arrStrDBVar[k]);
									if(k < arrStrDBVar.length) {
										strbNewSource.append("/");
									}
								}
							}

						}
						
						for(int k = 0; k < arrStrRef.length ; k++) {
							bRefIsInDB = false;
							for(int l = 0; l < arrStrDBVar.length && !bRefIsInDB; l++) {
								if(!vFinVar.contains(arrStrRef[k]) && 
									!isWholePresent(arrStrRef[k], arrStrDBVar, arrStrRef) &&
									(arrStrRef[k].equalsIgnoreCase(arrStrDBVar[l])
									|| ( arrStrDBVar[l].contains(".") && 
									arrStrRef[k].equalsIgnoreCase(arrStrDBVar[l].substring(0,
									arrStrDBVar[l].indexOf("."))) ) ) ) {
									strbNewSource.append(arrStrRef[k]);
									bRefIsInDB = true;
									vFinVar.add(arrStrRef[k]);
									if(k < arrStrRef.length) {
										strbNewSource.append("/");
									}
								}else if(!vFinVar.contains(arrStrDBVar[l]) && 
									!isWholePresent(arrStrDBVar[l], arrStrDBVar, arrStrRef) &&
									arrStrRef[k].contains(".") && 
									arrStrDBVar[l].equalsIgnoreCase(arrStrRef[k].substring(0,
									arrStrRef[k].indexOf(".")))) {
									strbNewSource.append(arrStrDBVar[l]);
									bRefIsInDB = true;
									vFinVar.add(arrStrDBVar[l]);
									if(k < arrStrRef.length) {
										strbNewSource.append("/");
									}
								}
							}


							
							
							
							if( (!vFinVar.contains(arrStrRef[k])
								|| (!vFinVar.contains(arrStrRef[k]) && arrStrRef[k].contains(".")
								&& !vFinVar.contains(arrStrRef[k].substring(0,
								arrStrRef[k].indexOf(".")))))
								&& !isWholePresent(arrStrRef[k], arrStrDBVar, arrStrRef)
								&& !bRefIsInDB) {
								//v: only add it if it is not yet there
								String strWholeNum = new String();
								
								
								
								if (!vFinVar.contains(arrStrRef[k])){
									strbNewSource.append(arrStrRef[k]);
									vFinVar.add(arrStrRef[k]);
									if(k < arrStrRef.length) {
										strbNewSource.append("/");
									}
								}
							}
						}
						
						while(strbNewSource.toString().endsWith("/")) {
							strbNewSource.deleteCharAt(strbNewSource.length()-1);
						}
												
						strbNewSource.append("]");
						
					}else {
						strbNewSource.append(oTmp.toString());
					}

					strbDBSource.append(oTmp.toString());
					strbDBSource.append(" ");
					strbNewSource.append(" ");
				}
			}
			
			//reset vFinVar
			vFinVar = new Vector(1);

			//check the selected db if there is already a template
			//that have whole no vars
			for(int i = 0; i < vTTemplatesFrDB.size(); i++) {
				tTmp = (TExtTemplate)vTTemplatesFrDB.get(i);
				for(int j = 0; j < tTmp.size(); j++) {
					oTmp = tTmp.get(j);
					if(oTmp instanceof TExtChunk) {
						vFinVar.clear();
						oTmpRef = (TExtChunk)tTargetRefinement.get(j);
						//check if var in tTargetRef is already in oTmp whole no: no need
						strbNewTarget.append("[");
						arrStrDBVar = stripSquareBrackets(oTmp.toString()).split("/");
						arrStrRef = stripSquareBrackets(oTmpRef.toString()).split("/");
						
						for(int k = 0; k < arrStrDBVar.length ; k++) {
							bRefIsInDB = false;
							for(int l = 0; l < arrStrRef.length && !bRefIsInDB; l++) {
								if(!vFinVar.contains(arrStrDBVar[k]) && 
									!isWholePresent(arrStrDBVar[k], arrStrDBVar, arrStrRef) &&
									(arrStrDBVar[k].equalsIgnoreCase(arrStrRef[l])
									|| (arrStrRef[l].contains(".") && 
									arrStrDBVar[k].equalsIgnoreCase(arrStrRef[l].substring(0,
									arrStrRef[l].indexOf(".")))))) {
									strbNewTarget.append(arrStrDBVar[k]);
									bRefIsInDB = true;
									vFinVar.add(arrStrDBVar[k]);
									if(k < arrStrDBVar.length) {
										strbNewTarget.append("/");
									}
								}else if(!vFinVar.contains(arrStrRef[l]) && 
									!isWholePresent(arrStrRef[l], arrStrDBVar, arrStrRef) &&
									arrStrDBVar[k].contains(".") && 
									arrStrRef[l].equalsIgnoreCase(arrStrDBVar[k].substring(0,
									arrStrDBVar[k].indexOf(".")))) {
									strbNewTarget.append(arrStrRef[l]);
									bRefIsInDB = true;
									vFinVar.add(arrStrRef[l]);
									if(k < arrStrDBVar.length) {
										strbNewTarget.append("/");
									}
								}
							}
							
							if((!vFinVar.contains(arrStrDBVar[k])
								|| (arrStrDBVar[k].contains(".")
								&& !vFinVar.contains(arrStrDBVar[k].substring(0,
								arrStrDBVar[k].indexOf(".")))))
								&& !isWholePresent(arrStrDBVar[k], arrStrDBVar, arrStrRef)
								&& !bRefIsInDB) {
								
								if(!vFinVar.contains(arrStrDBVar[k])){
									strbNewTarget.append(arrStrDBVar[k]);
									vFinVar.add(arrStrDBVar[k]);
									if(k < arrStrDBVar.length) {
										strbNewTarget.append("/");
									}									
								}

							}

						}
						
						for(int k = 0; k < arrStrRef.length ; k++) {
							bRefIsInDB = false;
							for(int l = 0; l < arrStrDBVar.length && !bRefIsInDB; l++) {
								if(!vFinVar.contains(arrStrRef[k]) &&
									!isWholePresent(arrStrRef[k], arrStrDBVar, arrStrRef) && 
									(arrStrRef[k].equalsIgnoreCase(arrStrDBVar[l])
									|| (arrStrDBVar[l].contains(".") && 
									arrStrRef[k].equalsIgnoreCase(arrStrDBVar[l].substring(0,
									arrStrDBVar[l].indexOf(".")))))) {
									strbNewTarget.append(arrStrRef[k]);
									bRefIsInDB = true;
									vFinVar.add(arrStrRef[k]);
									if(k < arrStrRef.length) {
										strbNewTarget.append("/");
									}
								}else if(!vFinVar.contains(arrStrDBVar[l]) && 
									!isWholePresent(arrStrDBVar[l], arrStrDBVar, arrStrRef) && 
									arrStrRef[k].contains(".") && 
									arrStrDBVar[l].equalsIgnoreCase(arrStrRef[k].substring(0,
									arrStrRef[k].indexOf(".")))) {
									strbNewTarget.append(arrStrDBVar[l]);
									bRefIsInDB = true;
									vFinVar.add(arrStrDBVar[l]);
									if(k < arrStrRef.length) {
										strbNewTarget.append("/");
									}
								}
							}

							if((!vFinVar.contains(arrStrRef[k])
								|| (arrStrRef[k].contains(".")
								&& !vFinVar.contains(arrStrRef[k].substring(0,
								arrStrRef[k].indexOf(".")))))
								&& !isWholePresent(arrStrRef[k], arrStrDBVar, arrStrRef)
								&& !bRefIsInDB) {
								
								if (!vFinVar.contains(arrStrRef[k])){
									strbNewTarget.append(arrStrRef[k]);
									vFinVar.add(arrStrRef[k]);
									if(k < arrStrRef.length) {
										strbNewTarget.append("/");
									}								
								}
							}
						}
						
						while(strbNewTarget.toString().endsWith("/")) {
							strbNewTarget.deleteCharAt(strbNewTarget.length()-1);
						}
						
						strbNewTarget.append("]");
					}else {
						strbNewTarget.append(oTmp.toString());
					}
					strbDBTarget.append(oTmp.toString());
					strbDBTarget.append(" ");
					strbNewTarget.append(" ");
				}
			}

//		update the db with the refinement vars in the vars of tDB
		if(vSTemplatesFrDB.size() > 0 && vTTemplatesFrDB.size() > 0) {

			DBConnector.update("UPDATE Templates SET "
				+ "template" + strSource + " = '" 
				+ strbNewSource.toString().trim().replace("'","''") + "', "
				+ "template" + strTarget + " = '" 
				+ strbNewTarget.toString().trim().replace("'","''") + "' "
				+ "WHERE "+ "template" + strSource + " = '" 
				+ strbDBSource.toString().trim().replace("'","''") + "' AND "
				+ "template" + strTarget + " = '" 
				+ strbDBTarget.toString().trim().replace("'","''") + "' AND "
				+ "compID" + strSource + " = '" 
				+ tSourceRefinement.getCompoundIdentifier() 
				+ "' AND compID" + strTarget + " = '" 
				+ tTargetRefinement.getCompoundIdentifier() 
				+ "'");

			tNewSource = new TExtTemplate(strbNewSource.toString().trim(),
						tSourceRefinement.getCompoundIdentifier());
			tNewTarget = new TExtTemplate(strbNewTarget.toString().trim(),
						tTargetRefinement.getCompoundIdentifier());

			atNewRefinement = new AlignedTemplate(tNewSource, tNewTarget);
		}

		oTmp = null;
		oTmpRef = null;
		tTmp = null;
		vSTemplatesFrDB = null;
		vTTemplatesFrDB = null;
		strbSRegexp = null;
		strbTRegexp = null;
		strbNewSource = null;
		strbNewTarget = null;
		rsDomain = null;

		return atNewRefinement;
	}

	protected boolean isWholePresent(String strToBeChecked, String[] arrStrDBVar,
		String[] arrStrRef) {
		boolean bFound = false;
		String strTmp = null;
		
		if(strToBeChecked.contains(".")) {
			strTmp = strToBeChecked.substring(0, strToBeChecked.indexOf("."));
			
			for(int i = 0; i < arrStrDBVar.length && !bFound; i++) {
				if(strTmp.equals(arrStrDBVar[i])) {
					bFound = true;
				}
			}
	
			for(int i = 0; i < arrStrRef.length && !bFound; i++) {
				if(strTmp.equals(arrStrRef[i])) {
					bFound = true;
				}
			}
			
		}
		
		return bFound;	
			
	}
										
	/**
	 *	This function strips the square brackets 
	 *	if the given domain still have them.
	 *
	 *	@param String strDomain (ex. [1], [1.1], [1/2.1])
	 *	@return String - with no square brackets at the start and at the end
	 *	(ex. 1, 1.1, 1/2.1)
	 */
	protected String stripSquareBrackets(String strDomain) {
		
		if(strDomain != null && strDomain.startsWith("[") && strDomain.endsWith("]")) {
			//strip the [] off
			strDomain = strDomain.substring(1, strDomain.length() - 1);
		}
		
		return strDomain;
	}

	//is the chunk a domain and is it part of the given domain?
	protected boolean isChunkADomAndInDomain(String strChunk, String strDom) {
		boolean bChunkIsDomAndInDomain = false;
		
		String[] arrStrDomains = stripSquareBrackets(strDom).split("/");
		String[] arrStrChunks = stripSquareBrackets(strChunk).split("/");
		
		for(int i = 0; i < arrStrDomains.length && !bChunkIsDomAndInDomain; i++) {
			for(int j = 0; j < arrStrChunks.length && !bChunkIsDomAndInDomain; j++) {
				if(arrStrDomains[i].equalsIgnoreCase(arrStrChunks[j])
					|| (arrStrChunks[j].contains(".") && 
					arrStrDomains[i].equalsIgnoreCase(arrStrChunks[j].substring(0,
					arrStrChunks[j].indexOf("."))))){
					bChunkIsDomAndInDomain = true;
				}
			}
		}
		
		arrStrDomains = null;
		arrStrChunks = null;
		
		return bChunkIsDomAndInDomain;
	}

	/**
	 *	This function retrieves the first whole number seen in the given domain.
	 *	For domains which does not contain a whole number, null would be returned.
	 *	
	 *	@param String strDomain (ex. [1], [1.1], [1/2.1], [1/1.1/2])
	 *	@return String - the first whole number seen in the given domain
	 *	(ex. 1, null, 1, 1)
	 */
	protected String getWholeNoDomain(String strDomain) {

		String[] arrStrDomains = stripSquareBrackets(strDomain).split("/");
		String strWholeNoDom = null;
		boolean bFound = false;
		
		for(int i = 0; i < arrStrDomains.length && !bFound; i++) {
			if(!arrStrDomains[i].contains(".")) {
				strWholeNoDom = arrStrDomains[i];
				bFound = true;
			}
		}
		
		arrStrDomains = null;
		
		return strWholeNoDom;
	}
	
	protected TExtTemplate modOrigin(TExtTemplate tSRefinement,
		TExtTemplate tTRefinement, String strOrigDom, String strNewDom,
		String strSource, String strTarget, boolean bModSource) {
		//find the strOrigDom and change it

		TExtTemplate tRefinement = null;
		String strLang = null;
		String strID = null;	
		Object oTmp;
		StringBuffer strbTRefinement = new StringBuffer();
		ResultSet rsID = null;
		
		if(bModSource) {
			tRefinement = tSRefinement;
			strLang = strSource;
		}else {
			tRefinement = tTRefinement;
			strLang = strTarget;
		}
		
//		System.out.println("tRefinement: "+tRefinement);
//		System.out.println("strOrigDom: "+strOrigDom);
//		System.out.println("strNewDom: "+strNewDom);

		strOrigDom = stripSquareBrackets(strOrigDom);
		strNewDom = stripSquareBrackets(strNewDom);
		
		for(int i = 0; i < tRefinement.size(); i++) {
			oTmp = tRefinement.get(i);
			if(oTmp instanceof TExtChunk) {
//				System.out.println("oTmp: "+oTmp);
				if(((TExtChunk)oTmp).toString().equalsIgnoreCase("["+strOrigDom+"]")) {
					strbTRefinement.append(((TExtChunk)oTmp).toString().replace("["+strOrigDom+"]",
						"["+strNewDom.substring(0,strNewDom.indexOf("."))+"/"+strOrigDom+"]"));
				}else {
					strbTRefinement.append(oTmp);
				}
			}else {
				strbTRefinement.append(oTmp);
			}
			
			strbTRefinement.append(" ");
		}

		rsID = DBConnector.select("SELECT templateID"
			+" FROM Templates WHERE "
			+ "template" + strSource + " = '" 
			+ tSRefinement.toString().replace("'","''") + "'"
			+ " AND template" + strTarget + " = '" 
			+ tTRefinement.toString().replace("'","''") + "'"
			+ " AND compID" + strSource + " = '" 
			+ tSRefinement.getCompoundIdentifier() + "'"
			+ " AND compID" + strTarget + " = '" 
			+ tTRefinement.getCompoundIdentifier() + "'");

		try {
			if(rsID.next()) {
				strID = rsID.getString("templateID");
			}
		} catch(SQLException sqle){
			sqle.printStackTrace();
		}

		//update the template of the origin
		DBConnector.update("UPDATE Templates SET "
			+ "template" + strLang + " = '" + strbTRefinement.toString().trim().replace("'","''") + "'"
			+ " WHERE templateID = '" 
			+ strID + "'");
		
		tRefinement = null;
		strLang = null;
		strID = null;
		oTmp = null;
		rsID = null;
	
		return new TExtTemplate(strbTRefinement.toString().trim());
	}

	protected TExtChunk modOrigin(TExtChunk tSChunk, TExtChunk tTChunk, 
		String strOrigDom, String strNewDom, String strSource, String strTarget,
		boolean bModSource) {
		//find the strOrigDom and change it

		TExtChunk tChunk = null;
		String strLang = null;
		String strID = null;	
		Object oTmp = null;
		StringBuffer strbChunk = new StringBuffer();
		ResultSet rsID = null;
	
		if(bModSource) {
			tChunk = tSChunk;
			strLang = strSource;
		}else {
			tChunk = tTChunk;
			strLang = strTarget;
		}

//		System.out.println("tChunk: "+tChunk);
//		System.out.println("strOrigDom: "+strOrigDom);
//		System.out.println("strNewDom: "+strNewDom);

		strOrigDom = stripSquareBrackets(strOrigDom);
		strNewDom = stripSquareBrackets(strNewDom);

		for(int i = 0; i < tChunk.size(); i++) {
			oTmp = tChunk.get(i);
			if(oTmp instanceof TExtChunk) {
//				System.out.println("oTmp: "+oTmp);
				if(((TExtChunk)oTmp).toString().equalsIgnoreCase("["+strOrigDom+"]")) {
					strbChunk.append(((TExtChunk)oTmp).toString().replace("["+strOrigDom+"]",
						"["+strNewDom.substring(0,strNewDom.indexOf("."))+"/"+strOrigDom+"]"));
				}else {
					strbChunk.append(oTmp);
				}
			}else {
				strbChunk.append(oTmp);
			}
			strbChunk.append(" ");
		}
		
		rsID = DBConnector.select("SELECT  chunkID"
			+ " FROM Chunks WHERE "
			+ "chunk" + strSource + " = '" 
			+ tSChunk.toString().replace("'","''") + "'"
			+ " AND chunk" + strTarget + " = '" 
			+ tTChunk.toString().replace("'","''") + "'"
			+ " AND compID" + strSource + " = '" 
			+ tSChunk.getCompoundIdentifier() + "'"
			+ " AND compID" + strTarget + " = '" 
			+ tTChunk.getCompoundIdentifier() + "'");

		try {
			if(rsID.next()) {
				strID = rsID.getString("chunkID");
			}
		} catch(SQLException sqle){
			sqle.printStackTrace();
		}

		//update the template of the origin
		DBConnector.update("UPDATE Chunks SET "
			+ "chunk" + strLang + " = '" + strbChunk.toString().trim().replace("'","''") + "'"
			+ " WHERE chunkID = '" 
			+ strID + "'");
		
		tChunk = null;
		strLang = null;
		strID = null;	
		oTmp = null;
		rsID = null;
		
		return new TExtChunk(strbChunk.toString().trim(), tSChunk.domain());
	}

	/**
	 *	This function processes the slashes in the given domain and retrieves 
	 *	all the chunks in the DB that have the same domain as the given domain.
	 *	
	 *	The given domain is separated by slashes.
	 *
	 *	@param String strDomain (ex. [1], [1.1], [1/2.1], [1/1.1/2])
	 *	@param String strSource - the source language (ex. English, Filipino)
	 *	@param String strTarget - the target language (ex. Filipino, English)
	 *	@return Vector - containing the Vector of source and Vector of target TExtChunk(s)
	 */
	protected Vector getChunksFrDB(String strDomain, 
		String strSource, String strTarget) {
		
		String[] arrStrDomains = stripSquareBrackets(strDomain).split("/");
		Vector vSrcAndTarChunksDB = null;
		Vector vSChunksFrDB = new Vector();
		Vector vTChunksFrDB = new Vector();
		Vector vTmpDB, vTmpS, vTmpT;
		
		for(int i = 0; i < arrStrDomains.length; i++) {
			vTmpDB = getChunksFrDBGivenADom(arrStrDomains[i], strSource, strTarget);
			
			if(vTmpDB != null) {
				vTmpS = (Vector)vTmpDB.get(0);
				vTmpT = (Vector)vTmpDB.get(1);
				
				for(int j = 0; j < vTmpS.size() && j < vTmpT.size(); j++) {
					vSChunksFrDB.add(vTmpS.get(j));
					vTChunksFrDB.add(vTmpT.get(j));
				}
			}
		}

		if(vSChunksFrDB.size() > 0 && vTChunksFrDB.size() > 0) {
			vSrcAndTarChunksDB = new Vector(2);
			vSrcAndTarChunksDB.add(vSChunksFrDB);
			vSrcAndTarChunksDB.add(vTChunksFrDB);
		}

		arrStrDomains = null;
		vSChunksFrDB = null;
		vTChunksFrDB = null;
		vTmpDB = null;
		vTmpS = null;
		vTmpT = null;

		return vSrcAndTarChunksDB;
	}

	/**
	 *	This function retrieves all the chunks in the DB that have the same 
	 *	domain as the given domain.
	 *	
	 *	@param String strDomain (ex. 1, 1.1)
	 *	@param String strSource - the source language (ex. English, Filipino)
	 *	@param String strTarget - the target language (ex. Filipino, English)
	 *	@return Vector - containing the Vector of source and Vector of target TExtChunk(s)
	 */
	protected Vector getChunksFrDBGivenADom(String strDomain,
		String strSource, String strTarget) {
		
		Vector vSrcAndTarChunksDB = null;
		Vector vSChunksFrDB = new Vector();
		Vector vTChunksFrDB = new Vector();
		ResultSet rsDomain = null;
		String strSrc = null;
		String strTar = null;
		float fDom;
		String strDom = null;
		String strCompIDS = null;
		String strCompIDT = null;
		
		if(strDomain.contains(".")) {
			rsDomain = DBConnector.select("SELECT chunk"+
				strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
				" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '"+strDomain+"%'");
		}else{
			rsDomain = DBConnector.select("SELECT chunk"+
				strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
				" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '" + strDomain + ".%'");
		}

		try {
			while(rsDomain.next()){
				strSrc = rsDomain.getString("chunk" + strSource);
				strTar = rsDomain.getString("chunk" + strTarget);
				fDom = rsDomain.getFloat("domain");
				strDom = fDom + "";
				strCompIDS = rsDomain.getString("compID" + strSource);
				strCompIDT = rsDomain.getString("compID" + strTarget);

				if(strSrc.trim().length() > 0 || strTar.trim().length() > 0) {
					vSChunksFrDB.add(tlu.adjustChunkCompound(new TExtChunk(strSrc, strDom, strCompIDS)));
					vTChunksFrDB.add(tlu.adjustChunkCompound(new TExtChunk(strTar, strDom, strCompIDT)));
				}
			}
		} catch(SQLException sqle){
			System.out.println("There is an SQL Error!");
			sqle.printStackTrace();
		}
		
		if(vSChunksFrDB.size() > 0 && vTChunksFrDB.size() > 0) {
			vSrcAndTarChunksDB = new Vector(2);
			vSrcAndTarChunksDB.add(vSChunksFrDB);
			vSrcAndTarChunksDB.add(vTChunksFrDB);
		}

		vSChunksFrDB = null;
		vTChunksFrDB = null;
		rsDomain = null;
		strSrc = null;
		strTar = null;
		strDom = null;
		strCompIDS = null;
		strCompIDT = null;

		return vSrcAndTarChunksDB;
	}

	//checks if the given TExtChunk is not of the form XY
	protected boolean chunkFrDBisNotXY(TExtChunk cChunksFrDB) {
		boolean bPresentXY = false;
		int nX = -1;

		for(int i = 0; i < cChunksFrDB.size() && !bPresentXY; i++) {
			if(cChunksFrDB.get(i) instanceof TExtChunk) {
				if(nX < 0) {
					nX = i;
				}else {
					if(nX == i - 1) {
						bPresentXY = true;
					}else {
						nX = i;
					}
				}
			}
		}
		
		return !bPresentXY;
	}
	
	/**
	 *	This function determines if the chunk in the database and the input 
	 *	chunk match each other.
	 *
	 *	The criteria of matching is that all constants in the database 
	 *	chunk corresponds to those constants in the input chunk.
	 *	Also at least one constant in the input chunk corresponds to
	 *	a variable in the database chunk.
	 *
	 *	For example:
	 *	[1]	Input Chunk: I love you
	 *		Database Chunk: [1] love [2]
	 *
	 *	[2] Input Chunk: They all love you very much.
	 *		Database Chunk: [1] love [2]
	 *
	 *	- ensures that every variable should have a constant
	 */
	protected boolean isMatch(TExtChunk cDB, TExtChunk cGiven){
		int nTrack = 0;
		Vector vPattern = new Vector();
		boolean bPrevIsChunk = false;
		boolean bHasMatch;
		boolean bContinue = true;
		for(int i = 0; i < cDB.size() && bContinue; i++){
			Object oCDBToken = cDB.get(i);
			if(oCDBToken instanceof String){
				bHasMatch = false;
				for(int j = nTrack; j < cGiven.size() && !bHasMatch; j++){
					Object oCGToken = cGiven.get(j);
					if(oCGToken.toString().compareTo(oCDBToken.toString()) == 0){
						nTrack = j;
						bHasMatch = true;
						bPrevIsChunk = false;
						do{
							vPattern.add("String");
							i++;
							nTrack++;
							if(i < cDB.size() && nTrack < cGiven.size()){
								oCDBToken = cDB.get(i);
								oCGToken = cGiven.get(nTrack);
								if(oCDBToken instanceof TExtChunk){
									i--;
									nTrack--;
									break;
								}
								else if(oCGToken.toString().compareTo(oCDBToken.toString()) != 0){
									bContinue = false;
									break;
								}
							}
							else{
								if(nTrack < cGiven.size())
									return false;
								break;
							}
						}while(i < cDB.size() && nTrack < cGiven.size());
						nTrack++;
					}
					else if(!bPrevIsChunk){
						bContinue = false;
						break;
					}
					else if(bPrevIsChunk)
						nTrack = j+1;
						
					oCGToken = null;
				}
			}
			else if(nTrack < cGiven.size()){
				vPattern.add("TExtChunk");
				nTrack++;
				bPrevIsChunk = true;
			}
			oCDBToken = null;
		}
		if(!bContinue)
			return false;
		if(nTrack < cGiven.size() && !bPrevIsChunk)
			return false;
		if(vPattern.size() != cDB.size()) {
			vPattern = null;
			return false;
		}
		return true;
	}
	
	/**
	 *	Look for the appropriate domains of those in the chunk 
	 *	given the chunk DB as its basis
	 *
	 *	@return Vector<AlignedChunk> contains the assigned Chunks of 
	 *	Source and Target with the appropriate Domain
	 *
	 *	if magmatch sa chunk -> pass the domain : 
	 *		means a there's a chunk in the DB similar to it
	 *	else -> null : means there is no chunk like that in the DB
	 *
	 *	Note:
	 *	possible optimization: storing all the values of the 
	 *	variable in the target and source
	 *
	 *	what if [1] and [1]
	 *	what if puro constants ung cSDB at cTDB?
	 *	what if mix ung cSDB at cTDB?
	 *	similarity for target
	 *	XY
	 *	1.1 -> 2
	 *
	 *	- what if variable lang ang magkaiba? sa isang chunk
	 *
	 *	- assumes: every var should have a value which is ensured in isMatch fcn
	 *
	 *	not tested
	 */
	protected Vector findChunkDomains(TExtChunk cSDB, TExtChunk cTDB,
		TExtChunk cSource, TExtChunk cTarget, String strSource, 
		String strTarget, Hashtable hAlignedWords, Vector vSourceChunks,
		Vector vTargetChunks) {

		
		String strOrigcSDBDomain = cSDB.domain();
		boolean asdf= false;
//		System.out.println("***********************************************");
//		System.out.println("FIND CHUNK DOM START");

		int nCount = 0;
		Vector vAlignedOriginAndChunksResult = new Vector(2),
			vAlignedChunksResult = null,
			vAlignedDBResult = null,
			vSPrelim = null,
			vTPrelim = null,
			vMatches = null;
		
		/**
		 *	know: cSDB matches cSource and cTDB matches cTarget
		 *	find: domain of cSource and cTarget
		 *	because there can be a variable in cSDB or cTDB
		 *
		 *	check if the cSDB == cSource && cTDB == cTarget
		 *	YES: exit... no need to look for chunk domains
		 *		 it's already in the DB
		 *	NO: continue
		 */
		if(cSDB.toString().equalsIgnoreCase(cSource.toString()) && 
			cTDB.toString().equalsIgnoreCase(cTarget.toString())) {
//			System.out.println("No need to add the chunk in the DB."
//				+ "It's already there.");
		}else {
			//count the number of variables
			//assumes: no. of var in cSDB == no. of var in cTDB
			nCount = countVar(cSDB);
			
			//get preliminary value of the variables in the source
			//consecutive vars are grouped as one here
			vSPrelim = getPrelimVarAndVal(cSDB, cSource);

			//get preliminary value of the variables in the target
			//consecutive vars are grouped as one here
			vTPrelim = getPrelimVarAndVal(cTDB, cTarget);
			
			//match those preliminary values of the variables of the 
			//source and target to each other through hAlignedWords
//			System.out.println("vSPrelim: "+vSPrelim);
//			System.out.println("vTPrelim: "+vTPrelim);

			vMatches = matchSourceAndTargetVars(cSDB, cSource, cTDB, cTarget,
				vSPrelim, vTPrelim, hAlignedWords);
//			System.out.println("vMatches: "+vMatches);

			//if vMatches does not equal the number of variables in the count
			//|| count is 0 then cDB can't be used as a basis for subchunks
			if(vMatches == null || vMatches.size() <= 0 ||
				nCount != vMatches.size()) {
				vAlignedOriginAndChunksResult = null;
			}else if(vAlignedOriginAndChunksResult != null){
				vAlignedOriginAndChunksResult.add(new Vector());
				vAlignedOriginAndChunksResult.add(new Vector());
				//check each source and target preliminary values for a
				//deeper subchunk (call this fcn again) if it does not 
				//have it then add it in the vector with the chunk origin
				//for-loop
				for(int nMatchIndex = 0; nMatchIndex < vMatches.size();
					nMatchIndex++) {
					AlignedChunk aMatch = (AlignedChunk)vMatches.get(nMatchIndex);
					TExtChunk cSrcSubChunk = (TExtChunk)aMatch.getSource();
					TExtChunk cTarSubChunk = (TExtChunk)aMatch.getTarget();
		 			String strDomain = cSrcSubChunk.domain();

	 				//get all chunks + domain in the DB with domain same as the domain of the Chunk
		 			Vector vChunksDB = getChunksFrDB(strDomain, strSource, strTarget);

		 			if(vChunksDB != null) {
						//storage of source TExtChunks in the DB
			 			Vector vSrcChunksFrDB = (Vector) vChunksDB.get(0);
						//storage of target TExtChunks in the DB
			 			Vector vTarChunksFrDB = (Vector) vChunksDB.get(1);
						boolean bMatch = false;

						TExtChunk cSrcChunksFrDB = null;
						TExtChunk cTarChunksFrDB = null;
						boolean bSChunkFrDBisNotXY;
						boolean bTChunkFrDBisNotXY;
						boolean bSIsMatch;
						boolean bTIsMatch;

						int nDBIndex = 0;
						//while all chunksDB && no match
						while(nDBIndex < vSrcChunksFrDB.size() && !bMatch) {
							//does the chunks in both source and target 
							//match to the chunks in the DB
							cSrcChunksFrDB = (TExtChunk)vSrcChunksFrDB.get(nDBIndex);
							cTarChunksFrDB = (TExtChunk)vTarChunksFrDB.get(nDBIndex);
							bSChunkFrDBisNotXY = chunkFrDBisNotXY(cSrcChunksFrDB);
							bTChunkFrDBisNotXY = chunkFrDBisNotXY(cTarChunksFrDB);
							bSIsMatch = isMatch(cSrcChunksFrDB, cSource);
							bTIsMatch = isMatch(cTarChunksFrDB, cTarget);

							if(bSChunkFrDBisNotXY &&
								bTChunkFrDBisNotXY &&
								bSIsMatch && 
								bTIsMatch) {
							
//					 			System.out.println("-->IS MATCH<--");
								//assign the domain of the chunks (S&T) based on the chunks in the DB
								Vector vAOriginAndChunksWithDomains = findChunkDomains((TExtChunk)vSrcChunksFrDB.get(nDBIndex),
											(TExtChunk)vTarChunksFrDB.get(nDBIndex), cSrcSubChunk, cTarSubChunk, strSource,
											strTarget, hAlignedWords, vSourceChunks, vTargetChunks);
								
								if(vAOriginAndChunksWithDomains != null &&
									vAOriginAndChunksWithDomains.size() > 0) {
									bMatch = true;
									//include those new in vAChunksWithDomains in the current vAlignedChunksResult
									vAlignedDBResult = (Vector)vAOriginAndChunksWithDomains.get(0);
									vAlignedChunksResult = (Vector)vAOriginAndChunksWithDomains.get(1);
									for(int i = 0; i < vAlignedChunksResult.size(); i++) {
										((Vector)vAlignedOriginAndChunksResult.get(0)).add(vAlignedDBResult.get(i));
										((Vector)vAlignedOriginAndChunksResult.get(1)).add(vAlignedChunksResult.get(i));
									}
								}//else: if vAChunksWithDomains or there are no subchunks to be added
								
								vAOriginAndChunksWithDomains = null;
							}
							nDBIndex++;
						}
						//no match means new domain
						if(!bMatch){
//			 				System.out.println("FCD: NEW DOMAIN! - no match");
			 				//strDomain: ex. if 1.1 
			 				//strSrcDomain: ex. 4 -> most previous so use this instead
			 				String strNewDom;

							if(getWholeNoDomain(strDomain) == null &&
								getWholeNoDomain(cSDB.domain()) != null) {
			 					strNewDom = checkDomain(cSDB.domain(),
									vSourceChunks, vTargetChunks, cSource, cTarget);

								//check if checkDomain changed in the whole number part
								//&& if X.Y is already in the DB: assume: X.Y already exists
				 				if(getWholeNoDomain(cSDB.domain()) == null &&
				 					!cSDB.domain().substring(0,
				 					cSDB.domain().indexOf(".")).equalsIgnoreCase(strNewDom.substring(0,
				 					strNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					cSDB = modOrigin(cSDB, cTDB, cSDB.domain(),
				 						strNewDom, strSource, strTarget, true);
				 					cTDB = modOrigin(cSDB, cTDB, cSDB.domain(),
				 						strNewDom, strSource, strTarget, false);
				 				}

			 				}else {
			 					strNewDom = checkDomain(strDomain,
									vSourceChunks, vTargetChunks, cSource, cTarget);

								//check if checkDomain changed in the whole number part
								//&& if X.Y is already in the DB: assume: X.Y already exists
				 				if(getWholeNoDomain(strDomain) == null &&
				 					!strDomain.substring(0,
				 					strDomain.indexOf(".")).equalsIgnoreCase(strNewDom.substring(0,
				 					strNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					cSDB = modOrigin(cSDB, cTDB, strDomain,
				 						strNewDom, strSource, strTarget, true);
				 					cTDB = modOrigin(cSDB, cTDB, strDomain,
				 						strNewDom, strSource, strTarget, false);
				 				}

			 				}

							cSrcSubChunk.setDomain(strNewDom);
			 				cTarSubChunk.setDomain(strNewDom);

			 				//repair the domain of the original chunk


			 				
				 			((Vector)vAlignedOriginAndChunksResult.get(0)).add(new AlignedChunk(cSDB, cTDB));
							((Vector)vAlignedOriginAndChunksResult.get(1)).add(new AlignedChunk(cSrcSubChunk, cTarSubChunk));
							
			 				strNewDom = null;
						}

			 			vSrcChunksFrDB = null;
						vTarChunksFrDB = null;
						cSrcChunksFrDB = null;
						cTarChunksFrDB = null;
						
					}
					//else: chunksDB == null
					else {
		 				//NO. strDomain: ex. if 1.1 || 1 but no match inside those
		 				//NO. strSrcDomain: ex. 4 -> most previous so use this instead
//		 				System.out.println("FCD: NEW DOMAIN! - no match in db");
			 			String strNewDom;

						if(getWholeNoDomain(strDomain) == null &&
							getWholeNoDomain(cSDB.domain()) != null) {
		 					strNewDom = checkDomain(cSDB.domain(),
								vSourceChunks, vTargetChunks, cSource, cTarget);

							//check if checkDomain changed in the whole number part
							//&& if X.Y is already in the DB: assume: X.Y already exists
			 				if(getWholeNoDomain(cSDB.domain()) == null &&
			 					!cSDB.domain().substring(0,
			 					cSDB.domain().indexOf(".")).equalsIgnoreCase(strNewDom.substring(0,
			 					strNewDom.indexOf(".")))) {
								//if it did then change the template or the chunk
			 					cSDB = modOrigin(cSDB, cTDB, cSDB.domain(),
			 						strNewDom, strSource, strTarget, true);
			 					cTDB = modOrigin(cSDB, cTDB, cSDB.domain(),
			 						strNewDom, strSource, strTarget, false);
			 				}
		 				}else {
		 					strNewDom = checkDomain(strDomain,
								vSourceChunks, vTargetChunks, cSource, cTarget);

							//check if checkDomain changed in the whole number part
							//&& if X.Y is already in the DB: assume: X.Y already exists
			 				if(getWholeNoDomain(strDomain) == null &&
			 					!strDomain.substring(0,
			 					strDomain.indexOf(".")).equalsIgnoreCase(strNewDom.substring(0,
			 					strNewDom.indexOf(".")))) {
								//if it did then change the template or the chunk
			 					cSDB = modOrigin(cSDB, cTDB, strDomain,
			 						strNewDom, strSource, strTarget, true);
			 					cTDB = modOrigin(cSDB, cTDB, strDomain,
			 						strNewDom, strSource, strTarget, false);
			 				}
		 				}

						cSrcSubChunk.setDomain(strNewDom);
		 				cTarSubChunk.setDomain(strNewDom);

			 			((Vector)vAlignedOriginAndChunksResult.get(0)).add(new AlignedChunk(cSDB, cTDB));
						((Vector)vAlignedOriginAndChunksResult.get(1)).add(new AlignedChunk(cSrcSubChunk, cTarSubChunk));

						strNewDom = null;
					}
					
					aMatch = null;
					cSrcSubChunk = null;
					cTarSubChunk = null;
		 			strDomain = null;
					vChunksDB = null;

				}//for-loop for vMatch
			}//else if strChunk is empty: don't know yet -> error?: should be impossible
		}
		
		if(vAlignedOriginAndChunksResult == null ||
			vAlignedOriginAndChunksResult.size() <= 0 || 
			((Vector)vAlignedOriginAndChunksResult.get(1)).size() <= 0) {
			vAlignedOriginAndChunksResult = null;
		}
		
//		System.out.println("***********************************************");
//		System.out.println(vAlignedOriginAndChunksResult+"\nFIND CHUNK DOM END");

		vAlignedChunksResult = null;
		vAlignedDBResult = null;
		vSPrelim = null;
		vTPrelim = null;
		vMatches = null;
		
		return vAlignedOriginAndChunksResult;
	}
	
	//counts the variable
	protected int countVar(TExtChunk cDB) {
		int nCount = 0;
		
		for(int i = 0; i < cDB.size(); i++) {
			if(cDB.get(i) instanceof TExtChunk) {
				nCount++;
			}
		}
		
		return nCount;
	}

	//var is from DB, val is from chunk
	//gets the vars' possible val through the constants
	//if the var is of the form XY all vars are in one Vector
	protected Vector getPrelimVarAndVal(TExtChunk cDB, TExtChunk cChunk) {
		
		boolean bCantAddSubchunks = false,
			bFoundNextConstant;
		int nChunkIndex = 0;
		Vector vPrelim = new Vector(),
			vValAndVar = null,
			vVariable = null,
			vValue = null;
		String strCurrentCDB = null,
			strCurrentChunk = null,
			strNextCDB = null,
			strNextChunk = null;
		
		for(int nDBIndex = 0; nDBIndex < cDB.size() &&
			nChunkIndex < cChunk.size() && !bCantAddSubchunks; nDBIndex++) {
			
			strCurrentCDB = cDB.get(nDBIndex) + "";

			//if the String in the ChunkDB is constant
			if(!strCurrentCDB.startsWith("[") && !strCurrentCDB.endsWith("]")) {
				strCurrentChunk = cChunk.get(nChunkIndex) + "";
				//if the constant does not match each other
				if(!strCurrentCDB.equalsIgnoreCase(strCurrentChunk)) {
					bCantAddSubchunks = true;
					vPrelim = null;
				}
				nChunkIndex++;
			}
			//if the String in the ChunkDB is a variable
			else {
				//expand variable
				bFoundNextConstant = false;
				vValAndVar = new Vector(2);
				vVariable = new Vector();
				vValue = new Vector();
				vVariable.add(strCurrentCDB);

				while(nDBIndex < cDB.size() && !bFoundNextConstant) {
					//if there is a next in cDB
					if(nDBIndex < cDB.size()-1) {
						strNextCDB = cDB.get(nDBIndex + 1) + "";
						//check if the next String in the ChunkDB is a constant
						if(!strNextCDB.startsWith("[") && 
							!strNextCDB.endsWith("]")) {
							//get those in between in the cChunk from current
							//index to the mark of cDB
							//not yet the last String && not equal to the next
							//String in the cDB which is a constant
						
							while(nChunkIndex < cChunk.size() && 
								!strNextCDB.equalsIgnoreCase(strCurrentChunk = new String(cChunk.get(nChunkIndex)
								+""))) {

								vValue.add(strCurrentChunk);
								nChunkIndex++;
							}
							
							vValAndVar.add(vVariable);
							vValAndVar.add(vValue);
							bFoundNextConstant = true;
						}
						//[X][Y] collects the variables
						else {
							vVariable.add(strNextCDB);
						}
					//if last
					}else {
						//get the Strings in the chunk before the 
						//constant in the chunkDB or the subchunk
						//not yet the last String && not equal to
						//the next String in the chunkDB 
						//which is a constant
						while(nChunkIndex < cChunk.size()) {
							strCurrentChunk = cChunk.get(nChunkIndex)+"";
						
							vValue.add(strCurrentChunk);
							nChunkIndex++;
						}

						vValAndVar.add(vVariable);
						vValAndVar.add(vValue);
						bFoundNextConstant = true;
					}//else if strChunk is empty: don't know yet -> error?: 
					//should be impossible
					nDBIndex++;
				}//while: find the next constant or the end in the cDB
				nDBIndex--;
				vPrelim.add(vValAndVar);
			}//else EXPAND VAR
		}//for-loop for the cDB
		
		vValAndVar = null;
		vVariable = null;
		vValue = null;
		strCurrentCDB = null;
		strCurrentChunk = null;
		strNextCDB = null;
		strNextChunk = null;

		if(vPrelim.size() <= 0) {
			vPrelim = null;
		}
		
		return vPrelim;
	}

	//assumes: source and target may match each 
	//other from the division of preliminary values
	protected Vector matchSourceAndTargetVars(TExtChunk cDB1, TExtChunk cChunk1,
		TExtChunk cDB2, TExtChunk cChunk2, Vector vPrelim1, Vector vPrelim2,
		Hashtable hAlignedWords) {

//		System.out.println("************************************");
//		System.out.println("START - matchSourceAndTargetVars");
	
		boolean bCantAddSubChunk = false;
			//bNoPrevMatches = false;
		int n2PrelimIndex;
		Vector vMatch = new Vector(),	//AlignedChunk: value and domain
			vVariable1 = null,
			vValue1 = null,
			vVariable2 = null,
			vValue2 = null,
			vN2PrelimIndexMatched = new Vector();
		String strVar1 = null;
		Vector vSrcSubChunk = null,
			vTarSubChunk = null,
			vSubChunkN2PrelimIndexMatched = null;
		
		if(vPrelim1 != null && vPrelim2 != null) {
			//note: vPrelim1.size() != vPrelim2.size() if with XY
			//in this case since only separate vars then they should be equal
			//store the n2PrelimIndex which is already matched to avoid redundancy
			for(int n1PrelimIndex = 0; n1PrelimIndex < vPrelim1.size() &&
				!bCantAddSubChunk; n1PrelimIndex++) {
				vVariable1 = (Vector)((Vector)vPrelim1.get(n1PrelimIndex)).get(0);
				vValue1 = (Vector)((Vector)vPrelim1.get(n1PrelimIndex)).get(1);
				//assume: v1/2Variable always has at least one value
				//if there is only one var in 1
				if(vVariable1.size() == 1) {
					strVar1 = stripSquareBrackets((String)vVariable1.get(0));
//					System.out.println("strVar1 : "+strVar1);
					//find the corresponding var in 2 (first instance)
					//checks for the existence of a particular variable
					n2PrelimIndex = getCorrespVarIndex(strVar1, vPrelim2);

					if(n2PrelimIndex >= 0) {
						vVariable2 = (Vector)((Vector)vPrelim2.get(n2PrelimIndex)).get(0);
						vValue2 = (Vector)((Vector)vPrelim2.get(n2PrelimIndex)).get(1);

						//if only one var
						if(vVariable2.size() == 1) {
							//check for alignment and find the suitable var
							vSrcSubChunk = (Vector)((Vector)vPrelim1.get(n1PrelimIndex)).get(1);
							vSubChunkN2PrelimIndexMatched = getAlignedValue(vVariable1,
										vValue1, vPrelim2, vN2PrelimIndexMatched,
										hAlignedWords, true);
							if(vSrcSubChunk != null && vSubChunkN2PrelimIndexMatched != null &&
								(vTarSubChunk = (Vector)vSubChunkN2PrelimIndexMatched.get(1)) != null) {
								TExtChunk cSrcSubChunk = new TExtChunk(tlu.toStringChunk(vSrcSubChunk), strVar1, tlu.makeCompoundIdentifier(vSrcSubChunk));
								TExtChunk cTarSubChunk = new TExtChunk(tlu.toStringChunk(vTarSubChunk), strVar1, tlu.makeCompoundIdentifier(vTarSubChunk));

								vMatch.add(new AlignedChunk(cSrcSubChunk, cTarSubChunk));
								vN2PrelimIndexMatched.add(((Integer)vSubChunkN2PrelimIndexMatched.get(0)));
							}else {
								//no XY
								//maybe the 2nd var is part of an XY or maybe
								//ambigiuous this will be addressed here or in
								//matchXY
								bCantAddSubChunk = true;
								vMatch = null;
							}
						}else {
							bCantAddSubChunk = true;
							vMatch = null;
						}
					}else {
						//the var1 is not in vPrelim2
						bCantAddSubChunk = true;
						vMatch = null;
					}
				}else {
					bCantAddSubChunk = true;
					vMatch = null;
				}
			}
		}
		
		if(vMatch != null && (vMatch.size() <= 0 || bCantAddSubChunk)) {
			vMatch = null;
		}
		
//		System.out.println("END - matchSourceAndTargetVars");
//		System.out.println("************************************");

		vVariable1 = null;
		vValue1 = null;
		vVariable2 = null;
		vValue2 = null;
		vN2PrelimIndexMatched = null;
		strVar1 = null;
		vSrcSubChunk = null;
		vTarSubChunk = null;
		vSubChunkN2PrelimIndexMatched = null;

		return vMatch;
	}
	
	//find the corresponding var in 2 (first instance)
	//assumes: strVar = [2] or strVar = [2.1] or strVar [3/4.1]
	protected int getCorrespVarIndex(String strVar, Vector vPrelim) {
		int nIndex = -1;
		boolean bFound = false;
		String strVariable = null;
		Vector vVariable = null;
		Vector vValue = null;
		
		for(int nPrelimIndex = 0; nPrelimIndex < vPrelim.size();
			nPrelimIndex++) {

			vVariable = (Vector)((Vector)vPrelim.get(nPrelimIndex)).get(0);
			vValue = (Vector)((Vector)vPrelim.get(nPrelimIndex)).get(1);
			
			for(int nVarIndex = 0; nVarIndex < vVariable.size() && !bFound;
				nVarIndex++) {
					
				strVariable = vVariable.get(nVarIndex) + "";
			
				if(strVariable.contains(strVar)) {
					nIndex = nVarIndex;
					bFound = true;
				}
			}
		}
		
		strVariable = null;
		vVariable = null;
		vValue = null;
		
		return nIndex;
	}

	//returns the value of a particular index
	protected String getValOfVar(int nPrelimIndex, Vector vPrelim) {
		Vector vVariable = (Vector)((Vector)vPrelim.get(nPrelimIndex)).get(0),
			vValue = (Vector)((Vector)vPrelim.get(nPrelimIndex)).get(1);
		StringBuffer strbValue = new StringBuffer();
		
		for(int i = 0; i < vValue.size(); i++) {
			strbValue.append(vValue.get(i));
			strbValue.append(" ");
		}
		
		
		String strValue = null;
		if(strbValue != null && strbValue.length() > 0) {
			strValue = strbValue.toString().trim();
		}
		
		vVariable = null;
		vValue = null;
		strbValue = null;
		
		return strValue;
	}
	
	//returns the aligned value of a corresponding var
	//compare vMatch
	//assumes: vVariable1 is only one var but vVariable2 may have n vars
	//XY alignment
	//previous match XY
	protected Vector getAlignedValue(Vector vVariable1, Vector vValue1,
		Vector vPrelim2, Vector vPrevPrelim2Matches, Hashtable hAlignedWords,
		boolean bIsSource) {
		
		boolean bCantAddSubChunk = false,
			bAligned = false;
		int nChosenIndex = -1;
		
		String strValue = null;
		Vector vAlignedIndex = new Vector(),	//AlignedTExtChunk: value and domain
			vAlignedValue = new Vector(),
			vVariable2 = null,
			vValue2 = null;					//possible XY
		
		if(vPrelim2 != null) {
			//note: vPrelim1.size() != vPrelim2.size()
			for(int n2PrelimIndex = 0; n2PrelimIndex < vPrelim2.size() &&
				!bCantAddSubChunk && !bAligned; n2PrelimIndex++) {
				vVariable2 = (Vector)((Vector)vPrelim2.get(n2PrelimIndex)).get(0);
				vValue2 = (Vector)((Vector)vPrelim2.get(n2PrelimIndex)).get(1);
				
				//what if XY? dpt meron lng na in common; ok lng sa ngayon kc wla munang XY
				if(!isIndexInVector(vPrevPrelim2Matches, n2PrelimIndex)
					&& isEquivVar(vVariable1, vVariable2)) {
					if(vVariable2.size() == 1) {
						//checkIfAligned
						//maybe all aligned
						//maybe not all r aligned bcoz of it being null
						if(bIsSource) {
							int nIndex = -1;
							boolean bFound = false;
							int nAligned = 0;
							StringBuffer strbAlWTarget = null;
							Vector vAlWTarget = null;
							for(int nVal1 = 0; nVal1 < vValue1.size(); nVal1++) {
								vAlWTarget = (Vector)hAlignedWords.get((String)vValue1.get(nVal1));
								if(vAlWTarget != null) {
									//search in value2
									//check if same order...
									for(int nVal2 = 0; nVal2 < vValue2.size() && !bFound; nVal2++) {
										//checkForDuplicateIndices
										if(!isIndexInVector(vAlignedIndex, nVal2)) {
											strbAlWTarget = new StringBuffer();
											for(int nAlWTarget = 0; nAlWTarget < vAlWTarget.size(); nAlWTarget++) {
												strbAlWTarget.append((String)vAlWTarget.get(nAlWTarget)+" ");
											}
											
											if(strbAlWTarget.toString().trim().equalsIgnoreCase((String)vValue2.get(nVal2))) {
												bFound = true;
												nIndex = nVal2;
											}
											if(nAligned == vAlWTarget.size()){
												bFound = true;
												nIndex = nVal2;
											}
										}//find another
									}
									//check if 
									if(bFound) {
										vAlignedIndex.add(new Integer(nIndex+""));
										bFound = false;
									}
								}else {
									vAlignedIndex.add(new Integer(nIndex+""));
								}
							}
							
							//???problem: what if source ung null at target meron - di align
							//ask kat: what if null sa target, ano ang sa source ng hash?
							//check for vals in vValue2 that has no alignment yet
							boolean bNotAligned = false;
							if(vAlignedIndex.size() < vValue2.size()) {
								for(int nVal2 = 0; nVal2 < vValue2.size() 
									&& !bNotAligned; nVal2++) {
									if(!isIndexInVector(vAlignedIndex, nVal2)) {
										//check: if may ganung word sa hash, kpg meron, then wrong 
										//so hindi aligned
										if(hAlignedWords.containsValue(((String) vValue2.get(nVal2)))) {
											bNotAligned = true;
										}else {
											vAlignedIndex.add(new Integer(nVal2));
										}
									}
								}
							}
							
							//if all in value2 is aligned then add its value 2 the buffer
							if(vAlignedIndex != null && vAlignedIndex.size() > 0
								&& vAlignedIndex.size() == vValue2.size()) {
								vAlignedValue = vValue2;
								bAligned = true;
								nChosenIndex = n2PrelimIndex;
							}

							strbAlWTarget = null;
							vAlWTarget = null;
						}
					}else {
						bCantAddSubChunk = true;
						vAlignedIndex = null;
					}
				}//else: it can't be aligned yet, maybe further on
			}
		}

		Vector vIndexNAlignedVal = new Vector();

		if(nChosenIndex >= 0 && vAlignedValue != null) {
			vIndexNAlignedVal.add(new Integer(nChosenIndex+""));
			vIndexNAlignedVal.add(vAlignedValue);
			vIndexNAlignedVal.trimToSize();
		}else {
			vIndexNAlignedVal = null;
		}

		strValue = null;
		vAlignedIndex = null;
		vAlignedValue = null;
		vVariable2 = null;
		vValue2 = null;

		return vIndexNAlignedVal;
	}
	
	protected boolean isIndexInVector(Vector vVector, int nIndex) {
		boolean bIsIn = false;
		
		if(vVector != null) {
			for(int i = 0; i < vVector.size() && !bIsIn; i++) {
				if(((Integer)vVector.get(i)).intValue() == nIndex) {
					bIsIn = true;
				}
			}
		}
		
		return bIsIn;
	}
	
	protected boolean isEquivVar(Vector vVariable1, Vector vVariable2) {
		boolean bEquiv = true;
		
		if(vVariable1 != null && vVariable2 != null && vVariable1.size() > 0
		   && vVariable2.size() > 0) {
			for(int i = 0; i < vVariable1.size() && bEquiv; i++) {
				if(!((String)vVariable1.get(i)).equalsIgnoreCase((String)vVariable2.get(i))) {
					bEquiv = false;
				}
			}
		}else {
			bEquiv = false;
		}
		
		return bEquiv;
	}

	/**
	 *	Assumption:
	 *		- the size of vSrcChunks at vTarChunks are equal
	 *
	 *	This function retrieves the highest domain in the domain of the given 
	 *	chunks.
	 *
	 *	@param Vector vSrcChunks
	 *	@param Vector vTarChunks
	 *	@return String containing the highest domain in the given chunks
	 *	(ex. 8, 8.5)
	 *
	 */
	protected String getMaxDomInChunks(Vector vSrcChunks, Vector vTarChunks, 
		TExtChunk cSource, TExtChunk cTarget) {

		float fMaxDom = -1;
		float fSDomain;
		float fTDomain;
		int nWholeDom;
		String strMaxDom = null,
			strSrcDom = null,
			strTarDom = null;
		TExtChunk cVSrc = null,
			cVTar = null;
		
		if(vSrcChunks != null && vTarChunks != null) {
			for(int i = 0; i < vSrcChunks.size(); i++) {
				cVSrc = (TExtChunk) vSrcChunks.get(i);
				cVTar = (TExtChunk) vTarChunks.get(i);
				
				if(cSource == null || cTarget == null ||
				!cVSrc.toString().equalsIgnoreCase(cSource.toString()) || 
				!cVTar.toString().equalsIgnoreCase(cTarget.toString())) {
					strSrcDom = stripSquareBrackets(cVSrc.domain());
					strTarDom = stripSquareBrackets(cVTar.domain());
					
					String[] arrStrSDom = strSrcDom.split("/");
					String[] arrStrTDom = strTarDom.split("/");
					
					for(int j = 0; j < arrStrSDom.length; j++) {
						if(arrStrSDom[j].contains(".")){
							System.out.println("strSrcDom "+strSrcDom);
							System.out.println("domains in arrStrSDom[j] "+ arrStrSDom[j]);
							
							nWholeDom = (new Integer(arrStrSDom[j].substring(0,arrStrSDom[j].indexOf(".")))).intValue();
							String strTmpMax = String.valueOf(fMaxDom);
							System.out.println("strTmpMax: "+ strTmpMax);
							System.out.println ("TEST "+strTmpMax.substring(strTmpMax.indexOf(".") + 1, strTmpMax.length()) );
							
							int nTmpMaxDec = (new Integer(strTmpMax.substring(strTmpMax.indexOf(".") + 1, strTmpMax.length()))).intValue();
							if(nWholeDom > nTmpMaxDec){
								fMaxDom = (new Float(arrStrSDom[j])).floatValue();
							}
							//check .X
							else if(nWholeDom == nTmpMaxDec){
								strTmpMax = new String(fMaxDom+"");
								nTmpMaxDec = (new Integer(strTmpMax.substring(arrStrSDom[j].indexOf(".") + 1, strTmpMax.length()))).intValue();
								int nDecDom = (new Integer(arrStrSDom[j].substring(arrStrSDom[j].indexOf(".") + 1, arrStrSDom[j].length()))).intValue();
								
								if(nDecDom > nTmpMaxDec) {
									fMaxDom = (new Float(arrStrSDom[j])).floatValue();
								}
							}
							strTmpMax = null;
						}
						//whole no
						else{
							nWholeDom = (new Integer(arrStrSDom[j])).intValue();
							if(nWholeDom > fMaxDom){
								fMaxDom = (new Float(arrStrSDom[j])).floatValue();
							}
						}
					}
					
					for(int j = 0; j < arrStrTDom.length; j++) {
						if(arrStrTDom[j].contains(".")){
							nWholeDom = (new Integer(arrStrTDom[j].substring(0,arrStrTDom[j].indexOf(".")))).intValue();
							if(nWholeDom > fMaxDom){
								fMaxDom = (new Float(arrStrTDom[j])).floatValue();
							}
							//check .X
							else if(nWholeDom == fMaxDom){
								String strTmpMax = String.valueOf(fMaxDom);
								int nTmpMaxDec = (new Integer(strTmpMax.substring(arrStrTDom[j].indexOf(".") + 1, strTmpMax.length()))).intValue();
								int nDecDom = (new Integer(arrStrTDom[j].substring(arrStrTDom[j].indexOf(".") + 1, arrStrTDom[j].length()))).intValue();
								
								if(nDecDom > nTmpMaxDec) {
									fMaxDom = (new Float(arrStrTDom[j])).floatValue();
								}
								strTmpMax = null;
							}
						}
						//whole no
						else{
							nWholeDom = (new Integer(arrStrTDom[j])).intValue();
							if(nWholeDom > fMaxDom){
								fMaxDom = (new Float(arrStrTDom[j])).floatValue();
							}
						}
					}

					arrStrSDom = null;
					arrStrTDom = null;
					
				}
			}
		}
		
		if(fMaxDom > 0) {
			strMaxDom = fMaxDom + "";
			
			if(strMaxDom.endsWith(".0")) {
				strMaxDom = strMaxDom.substring(0,
					strMaxDom.indexOf("."));
			}
		}

		strSrcDom = null;
		strTarDom = null;
		cVSrc = null;
		cVTar = null;

		return strMaxDom;
	}

	/**
	 *	This function retrieves the highest domain in the database.
	 *
	 *	@return String containing the highest domain in the DB
	 *	(ex. 8.5)
	 */
	protected String getMaxDomInDB() {
		int nMax = Integer.parseInt(tlu.returnNextChunk())-1;
		
//		System.out.println("nMax -->" + nMax);
		
		String strMaxDomain = null;

		if(nMax == 0)
			return strMaxDomain;
		
		String strQuery = "SELECT domain FROM Chunks WHERE domain LIKE '" + nMax + ".%'";

		int nMaxLength = 0;
		Vector vDomains = new Vector();
		try{
			ResultSet rs = DBConnector.select(strQuery);
			while(rs.next()){
				String strDomain = rs.getString("domain");
				
//				System.out.println("strDomain -->" + strDomain);
				
				if(strDomain == null)
					break;
				int nDomLength = strDomain.length();
				if(nDomLength > nMaxLength){
//					System.out.println(nDomLength + ">" + nMaxLength);
					nMaxLength = nDomLength;
					vDomains.clear();
					vDomains.add(strDomain);
//					System.out.println("vDomains-->" + vDomains);
				}
				else if(nDomLength == nMaxLength){
//					System.out.println(nDomLength + "==" + nMaxLength);
					vDomains.add(strDomain);
//					System.out.println("vDomains-->" + vDomains);
				}
//				else
//					System.out.println(nDomLength + "<" + nMaxLength);
			}
			
			rs = null;
		}catch(Exception sqle){
			System.out.println("There is an SQL Error!");
			sqle.printStackTrace();
		}
		
		int nMaxDomain = 0;
//		System.out.println("vDomains-->" + vDomains);
		for(Iterator it = vDomains.iterator(); it.hasNext(); ){
//			System.out.println("nMaxDomain -->" + nMaxDomain);
//			System.out.println("strMaxDomain -->" + strMaxDomain);
			String strTemp = (String)it.next();
//			System.out.println("strTemp -->" + strTemp);

			int nTemp = Integer.parseInt(strTemp.substring(strTemp.indexOf(".") + 1, strTemp.length()));
//			System.out.println("nTemp -->" + nTemp);
			if(nTemp > nMaxDomain){
//				System.out.println("nTemp > nMaxDomain");
				nMaxDomain = nTemp;
				strMaxDomain = strTemp;
			}
//			else{
//				System.out.println("nTemp <= nMaxDomain");
//			}
		}
		
		strQuery = null;
		vDomains = null;

		return strMaxDomain;
	}

	/**
	 *	? Will depend on addChunkToDB
	 *
	 *	Assumptions:
	 *		- strDomain contains only one domain, no slashes
	 *
	 *	This function provides the new domain.
	 *	
	 *	For a given domain with a whole number, the next to 
	 *	the highest domain under that domain would be returned.
	 *	(ex. in the DB: 1.1, 1.2, 1.3; param: 1; returned: 1.4)
	 *
	 *	For a given domain with a decimal point, the next whole number with
	 *	respect to the domains in the database and in all the given chunks
	 *	with .1 would be returned.
	 *	(ex. in the DB: 1.1, 1.2, 1.3, 2.1, 2.2; param: 2.2; returned: 3.1)
	 *
	 */
	protected String getNewDomain(String strDomain, Vector vSrcChunks,
		Vector vTarChunks, TExtChunk cSource, TExtChunk cTarget) {
		
		String strMaxDom = null;
		String strNewDom = null;
		ResultSet rsDomain = null;
		boolean bFoundBlank = false;
		
		strDomain = stripSquareBrackets(strDomain);
		
//		System.out.println("strDomain: "+strDomain);
		
		if(strDomain != null && !strDomain.contains(".")) {
			String strQuery = "SELECT chunkEnglish , chunkFilipino , domain"
				+" FROM Chunks WHERE domain LIKE '" + strDomain + ".%'";
	
			int nMaxLength = 0;
			Vector vDomains = new Vector();
			try{
				ResultSet rs = DBConnector.select(strQuery);
				while(rs.next() && !bFoundBlank) {
					String strDBDomain = rs.getString("domain");
					String strSChunk = rs.getString("chunkEnglish");
					String strTChunk = rs.getString("chunkFilipino");
					
					//check for blank domains
					if(strSChunk.equalsIgnoreCase(" ") &&
						strTChunk.equalsIgnoreCase(" ")) {
						bFoundBlank = true;
						strNewDom = strDBDomain;
					}else {
//						System.out.println("strDomain -->" + strDBDomain);
						
						if(strDBDomain == null)
							break;
						int nDomLength = strDBDomain.length();
						if(nDomLength > nMaxLength){
//							System.out.println(nDomLength + ">" + nMaxLength);
							nMaxLength = nDomLength;
							vDomains.clear();
							vDomains.add(strDBDomain);
//							System.out.println("vDomains-->" + vDomains);
						}
						else if(nDomLength == nMaxLength){
//							System.out.println(nDomLength + "==" + nMaxLength);
							vDomains.add(strDBDomain);
//							System.out.println("vDomains-->" + vDomains);
						}
//						else
//							System.out.println(nDomLength + "<" + nMaxLength);
					}

					strDBDomain = null;
					strSChunk = null;
					strTChunk = null;
				}
				
				rs = null;
			}catch(Exception sqle){
				System.out.println("There is an SQL Error!");
				sqle.printStackTrace();
			}
			
			if(!bFoundBlank) {
				int nMaxDomain = 0;
//				System.out.println("vDomains-->" + vDomains);
				for(Iterator it = vDomains.iterator(); it.hasNext(); ){
//					System.out.println("nMaxDomain -->" + nMaxDomain);
//					System.out.println("strMaxDomain -->" + strMaxDom);

					String strTemp = (String)it.next();

//					System.out.println("strTemp -->" + strTemp);

					int nTemp = Integer.parseInt(strTemp.substring(strTemp.indexOf(".")+1, strTemp.length()));
//					System.out.println("nTemp -->" + nTemp);
					if(nTemp > nMaxDomain){
//						System.out.println("nTemp > nMaxDomain");
						nMaxDomain = nTemp;
						strMaxDom = strTemp;
					}
//					else{
//						System.out.println("nTemp <= nMaxDomain");
//					}
					
					strTemp = null;
				}

				if(strMaxDom == null) {
					strNewDom = strDomain + ".1";
				}else {
					strNewDom = strDomain + "." + (Integer.parseInt(strMaxDom.substring(strMaxDom.indexOf(".") + 1, strMaxDom.length())) + 1);
				}
//				System.out.println("strMaxDom: "+strMaxDom);
			}
			
			strQuery = null;
			vDomains = null;

		}else {
			String strMaxInChunks = getMaxDomInChunks(vSrcChunks, vTarChunks,
										cSource, cTarget);
			String strMaxInDB = getMaxDomInDB();
			
//			System.out.println("strMaxInChunks: "+strMaxInChunks);
//			System.out.println("strMaxInDB: "+strMaxInDB);
			
			if(strMaxInChunks == null && strMaxInDB == null) {
				strNewDom = "1.1";
			}else if(strMaxInChunks == null && strMaxInDB != null) {
				strNewDom = (Integer.parseInt(strMaxInDB.substring(0, strMaxInDB.indexOf("."))) + 1) + ".1";
			}else if(strMaxInChunks != null && strMaxInDB == null) {
				strNewDom = (Integer.parseInt(strMaxInChunks.substring(0, strMaxInChunks.indexOf("."))) + 1) + ".1";
			}else {
				if((new Float(strMaxInChunks)).floatValue() >= (new Float(strMaxInDB)).floatValue()) {
					strNewDom = (Integer.parseInt(strMaxInChunks.substring(0, strMaxInChunks.indexOf("."))) + 1) + ".1";
				}else {
					strNewDom = (Integer.parseInt(strMaxInDB.substring(0, strMaxInDB.indexOf("."))) + 1) + ".1";
				}
			}
			
			strMaxInChunks = null;
			strMaxInDB = null;
		}

		int nDecimalPt = (new Integer (strNewDom.substring(strNewDom.indexOf(".") + 1,
			strNewDom.length()))).intValue();

//		System.out.println("strNewDom: "+strNewDom);
			
		if(strNewDom.contains(".") && nDecimalPt % 10 == 0) {
			strNewDom = strNewDom.substring(0, strNewDom.indexOf(".") + 1) + (nDecimalPt + 1);
		}

		strMaxDom = null;
		rsDomain = null;
		
		return strNewDom;
	}

	/**
	 *	? Will depend on addChunkToDB
	 *
	 *	This function ensures that the domain is of the form X.Y.
	 *	
	 *	This function is used when adding chunks in the DB.
	 *
	 */
	protected String checkDomain(String strDomain, Vector vSrcChunks,
		Vector vTarChunks, TExtChunk cSource, TExtChunk cTarget) {
		String strNew = getNewDomain(getWholeNoDomain(strDomain), vSrcChunks,
							vTarChunks, cSource, cTarget);
//		System.out.println("checkDomain: "+strNew);
		return strNew;
	}

	/**
	 *	Adds those which are in the Vector of AlignedChunk to the DB using
	 *	addChunkToDB()
	 *  vmod
	 */
	protected boolean addAlignedChunksToDB(Vector vAlignedOriginAndChunks,
		String strSource, String strTarget, Vector vSourceChunks, Vector vTargetChunks) {

		boolean bCheck = false;
		Vector vAlignedOrigin = null;
		Vector vAlignedChunks = null;
		Object oTmp = null;

		if(vAlignedOriginAndChunks != null && vAlignedOriginAndChunks.size() == 2) {
			vAlignedOrigin = (Vector)vAlignedOriginAndChunks.get(0);
			vAlignedChunks = (Vector)vAlignedOriginAndChunks.get(1);
			bCheck = true;

			for(int i = 0; i < vAlignedChunks.size() && bCheck; i++) {
				oTmp = vAlignedOrigin.get(i);
				
				if(oTmp instanceof AlignedChunk) {
					bCheck = addChunkToDB(((AlignedChunk)vAlignedChunks.get(i)).getSource(), 
						((AlignedChunk)vAlignedChunks.get(i)).getTarget(),
						strSource, strTarget,
						((AlignedChunk)oTmp).getSource(),
						((AlignedChunk)oTmp).getTarget(),
						vSourceChunks, vTargetChunks);
					/* try modify parent here.
					 * modify parent should generalize parent: 
					 * [5/3.3/3.2], 5 should be removed since no child of 5 is in domain
					 * duplicates?
					 */					
					modifyParentDomain(  ((AlignedChunk)oTmp).getSource(),((AlignedChunk)oTmp).getTarget(),"","");
					
				}else {
					bCheck = addChunkToDB(((AlignedChunk)vAlignedChunks.get(i)).getSource(), 
						((AlignedChunk)vAlignedChunks.get(i)).getTarget(),
						strSource, strTarget,
						((AlignedTemplate)oTmp).getSource(),
						((AlignedTemplate)oTmp).getTarget(),
						vSourceChunks, vTargetChunks);
					/* try modify parent here.
					 * modify parent should generalize parent: 
					 * [5/3.3/3.2], 5 should be removed since no child of 5 is in domain
					 * duplicates?
					 */					
					modifyParentDomain(  ((AlignedTemplate)oTmp).getSource(),((AlignedTemplate)oTmp).getTarget(),"","");
				}
				
				oTmp = null;
			}
			vAlignedOrigin = null;
			vAlignedChunks = null;
		}

		return bCheck;
	}
	
	
	

	/**
	 *	This function adds the source and target chunk to the DB & ensures no 
	 *	duplication
	 *	
	 *	not tested
	 * Object oSOrigin, Object oTOrigin: TExtTemplate or TExtChunk
	 * 
	 */
	protected boolean addChunkToDB(TExtChunk cSrcChunk, TExtChunk cTarChunk,
		String strSource, String strTarget, Object oSOrigin, Object oTOrigin,
		Vector vSourceChunks, Vector vTargetChunks) {
		boolean bRefinementDone = false;
		
		if (TExtLearner.doChunkRefinement)
		bRefinementDone=cr.chunkRefinement( new AlignedChunk(cSrcChunk, cTarChunk), (Vector)oSOrigin, (Vector)oTOrigin, null);
		if (bRefinementDone) {
			return true;
		}
		
		
		boolean bCheck = true;
		String strDomain = cSrcChunk.domain();
		ResultSet rsDuplicate = null;
	
//		System.out.println("************ADDING CHUNKS TO DB************");
//		System.out.println("addChunkToDB\ncSrcChunk: "+cSrcChunk.toString()+
//		"\ncTarChunk: "+
//		cTarChunk.toString()+"\nstrDomain: "+strDomain
//		+"\noSrc: "+oSOrigin+"\noTar: "+oTOrigin);

		//ensures: chunks to be added are not empty
		if(cSrcChunk.size() > 0 && cTarChunk.size() > 0) {
			if(strDomain.contains(".")) {
				rsDuplicate = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = '" 
					+ cSrcChunk.toString().replace("'","''") + "'"
					+ " AND chunk" + strTarget + " = '" 
					+ cTarChunk.toString().replace("'","''")+ "'"
					+ " AND domain LIKE '"
					+ strDomain.substring(0,strDomain.indexOf('.')) + ".%'"
					+ " AND compID" + strSource + " = '"
					+ cSrcChunk.getCompoundIdentifier() + "'"
					+ " AND compID" + strTarget + " = '"
					+ cTarChunk.getCompoundIdentifier() + "'");
			}else {
				rsDuplicate = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = '" 
					+ cSrcChunk.toString().replace("'","''") + "'"
					+ " AND chunk" + strTarget + " = '" 
					+ cTarChunk.toString().replace("'","''")+ "'"
					+ " AND domain LIKE '"
					+ strDomain + ".%'"
					+ " AND compID" + strSource + " = '"
					+ cSrcChunk.getCompoundIdentifier() + "'"
					+ " AND compID" + strTarget + " = '"
					+ cTarChunk.getCompoundIdentifier() + "'");
			}
			
			try {
				//check for duplicates && source domain == target domain
				if(!rsDuplicate.next() && 
					cSrcChunk.domain().equalsIgnoreCase(cTarChunk.domain()) &&
					!checkDuplicateChunk(cSrcChunk, cTarChunk,
					strSource, strTarget, oSOrigin, oTOrigin,
					vSourceChunks, vTargetChunks) &&
					!checkBlankInDomain(cSrcChunk, cTarChunk,
					strSource, strTarget, vSourceChunks, vTargetChunks)) {
					
					int nCnstCntS = 0, nCnstCntT = 0;
					
					for(Iterator it = cSrcChunk.iterator(); it.hasNext(); )
						if(it.next() instanceof String)
							nCnstCntS++;
					for(Iterator it = cTarChunk.iterator(); it.hasNext(); )
						if(it.next() instanceof String)
							nCnstCntT++;
						
					String strUpdateChunk = "INSERT INTO Chunks"
						+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
						+ "compID" + strSource + ", compID" + strTarget + ", "
						+ "constCnt" + strSource.substring(0, 3) + ", "
						+ "constCnt" + strTarget.substring(0, 3) + ") "
						+ "VALUES ('" + cSrcChunk.toString().replace("'","''")
						+ "', '"
						+ cTarChunk.toString().replace("'","''")
						+ "', '"
						+ strDomain
						+ "', '" 
						+ cSrcChunk.getCompoundIdentifier()
						+ "', '" 
						+ cTarChunk.getCompoundIdentifier()
						+ "', " 
						+ nCnstCntS + ", " + nCnstCntT + ")";
					
					bCheck = DBConnector.update(strUpdateChunk);
					
					AlignedChunk acChunk = new AlignedChunk(cSrcChunk, cTarChunk);
					vTRChunks.add(acChunk);
//					System.out.println("Inserting: SUCCESSFUL!");
				}else {
					//No need to add the chunk, it's already in the DB
//					System.out.println("Inserting: NO NEED!");
					
//--------------->>>check if all doms of the templates are in the db
					checkDBDomainExistence(oSOrigin, oTOrigin, strSource, strTarget);
				}
			} catch(SQLException sqle){
				System.out.println("There was an error while inserting the"
				+" chunks from refinement in the database.");
				bCheck = false;
			}
		}
		
		strDomain = null;
		rsDuplicate = null;

		return bCheck;
	}

	/*
	 *	This fcn checks if the domain exists in the chunks db
	 */
	protected void checkDBDomainExistence(Object oSOrigin, Object oTOrigin,
		String strSource, String strTarget) {
		
		boolean bCheck;

		Object oTmp = null;
		String strTmpDom = null;
		String[] arrStrDomains = null;
		ResultSet rsDomain = null;

		if(oSOrigin instanceof TExtTemplate) {
			for(int i = 0; i < ((TExtTemplate)oSOrigin).size(); i++) {
				oTmp = ((TExtTemplate)oSOrigin).get(i);
				if(oTmp instanceof TExtChunk) {
					arrStrDomains = stripSquareBrackets(((TExtChunk)oTmp).toString()).split("/");
					//check each variable if it contains at least one entry in db
					for(int j = 0; j < arrStrDomains.length; j++) {
						
						if(arrStrDomains[j].contains(".")) {
							rsDomain = DBConnector.select("SELECT chunk"+
								strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
								" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '"+arrStrDomains[j]+"%'");
						}else{
							rsDomain = DBConnector.select("SELECT chunk"+
								strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
								" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '" + arrStrDomains[j] + ".%'");
						}
				
						try {
							//if one is not found, add a blank in that variable
							if(!rsDomain.next()){
//System.out.println(arrStrDomains[j]+" is not in the DB");
								if(!arrStrDomains[j].contains("."))
									arrStrDomains[j] += ".1";
									
							/*	bCheck = DBConnector.update("INSERT INTO Chunks"
									+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
									+ "compID" + strSource + ", compID" + strTarget + ", "
									+ "constCnt" + strSource.substring(0, 3) + ", "
									+ "constCnt" + strTarget.substring(0, 3) + ") "
									+ "VALUES (' ', ' ', '"
									+ arrStrDomains[j]
									+ "', 'none', 'none', 0, 0)");
									*/
								bCheck = true;
							}
						} catch(SQLException sqle){
							System.out.println("There is an SQL Error!");
							sqle.printStackTrace();
						}
					}
				}
			}
		}else if(oSOrigin instanceof TExtChunk) {
			for(int i = 0; i < ((TExtChunk)oSOrigin).size(); i++) {
				oTmp = ((TExtChunk)oSOrigin).get(i);
				if(oTmp instanceof TExtChunk) {
					arrStrDomains = stripSquareBrackets(((TExtChunk)oTmp).toString()).split("/");
					//check each variable if it contains at least one entry in db
					for(int j = 0; j < arrStrDomains.length; j++) {
						
						if(arrStrDomains[j].contains(".")) {
							rsDomain = DBConnector.select("SELECT chunk"+
								strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
								" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '"+arrStrDomains[j]+"%'");
						}else{
							rsDomain = DBConnector.select("SELECT chunk"+
								strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
								" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '" + arrStrDomains[j] + ".%'");
						}
				
						try {
							//if one is not found, add a blank in that variable
							if(!rsDomain.next()){

								if(!arrStrDomains[j].contains("."))
									arrStrDomains[j] += ".1";
								/*
								bCheck = DBConnector.update("INSERT INTO Chunks"
									+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
									+ "compID" + strSource + ", compID" + strTarget + ", "
									+ "constCnt" + strSource.substring(0, 3) + ", "
									+ "constCnt" + strTarget.substring(0, 3) + ") "
									+ "VALUES (' ', ' ', '"
									+ arrStrDomains[j]
									+ "', 'none', 'none', 0, 0)");
									*/
								bCheck=true;
							}
						} catch(SQLException sqle){
							System.out.println("There is an SQL Error!");
							sqle.printStackTrace();
						}
					}
				}
			}
		}
	}

	/**
	 *	This function check for blanks in the domain.
	 *
	 *	if there's blank,
	 *		use update
	 *		return true
	 *	else
	 *		return false
	 *
	 */
	protected boolean checkBlankInDomain(TExtChunk cSrcChunk,
		TExtChunk cTarChunk, String strSource, String strTarget,
		Vector vSourceChunks, Vector vTargetChunks) {
			
		boolean bCheck = false;
		String strDomain = cSrcChunk.domain();
		ResultSet rsDuplicate = null;
		
		if(cSrcChunk.size() > 0 && cTarChunk.size() > 0) {
			if(strDomain.contains(".")) {
				rsDuplicate = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = ' '"
					+ " AND chunk" + strTarget + " = ' '"
					+ " AND domain LIKE '" + strDomain + "%'");
			}else {
				rsDuplicate = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = ' '"
					+ " AND chunk" + strTarget + " = ' '"
					+ " AND domain LIKE '" + strDomain + ".%'");
			}
			
			try {
				if(rsDuplicate.next()) {
					
					int nCnstCntS = 0, nCnstCntT = 0;
					
					for(Iterator it = cSrcChunk.iterator(); it.hasNext(); )
						if(it.next() instanceof String)
							nCnstCntS++;
					for(Iterator it = cTarChunk.iterator(); it.hasNext(); )
						if(it.next() instanceof String)
							nCnstCntT++;
					
					bCheck = DBConnector.update("UPDATE Chunks SET "
						+ "chunk" + strSource + " = '" + cSrcChunk.toString().replace("'","''") + "' , "
						+ "chunk" + strTarget + " = '" + cTarChunk.toString().replace("'","''") + "' , "
						+ "domain = '" 
						+ cSrcChunk.domain()
						+ "' , "
						+ "compID" + strSource + " = '" + cSrcChunk.getCompoundIdentifier() + "' , "
						+ "compID" + strTarget + " = '" + cTarChunk.getCompoundIdentifier() + "' , "
						+ "constCnt" + strSource.substring(0, 3) + " = '" + nCnstCntS + "' , "
						+ "constCnt" + strTarget.substring(0, 3) + " = '" + nCnstCntT + "' "
						+ "WHERE chunkID = '" + rsDuplicate.getString("chunkID") + "'");

//					System.out.println("Inserting: SUCCESSFUL!");
				}
			} catch(SQLException sqle){
				System.out.println("There was an error while inserting the"
				+" chunks from refinement in the database.");
			}
		}
		
		return bCheck;
	}

	/**
	 *	This function checks for duplicate chunks in the DB with a different
	 *	domain.
	 *
	 *	oSOrigin and oTOrigin can either be a template or a chunk assumes that
	 *	cSrcChunk and cTarChunk contains the original domain meaning that
	 *	domain is also the domain to be replaced in the oSOrigin and oTOrigin
	 *	
	 *	not tested	
	 */
	protected boolean checkDuplicateChunk(TExtChunk cSrcChunk, TExtChunk cTarChunk,
		String strSource, String strTarget, Object oSOrigin, Object oTOrigin,
		Vector vSourceChunks, Vector vTargetChunks) {
			
		boolean bDuplicate = false, bCheck = false;

//		System.out.println("*****UPDATE DUPLICATE CHUNKS*****");

		ResultSet rsDuplicate = DBConnector.select("SELECT domain FROM Chunks WHERE "
			+ "chunk" + strSource + " = '" 
			+ cSrcChunk.toString().replace("'","''") + "'"
			+ " AND chunk" + strTarget + " = '" 
			+ cTarChunk.toString().replace("'","''")+ "'"
			+ " AND compID" + strSource + " = '"
			+ cSrcChunk.getCompoundIdentifier() + "'"
			+ " AND compID" + strTarget + " = '"
			+ cTarChunk.getCompoundIdentifier() + "'");
		String strDomain = null;
		Object oTmp = null;
		ResultSet rsDuplicateBlank = null;
					
		try {
			//check for duplicate chunk
			while(rsDuplicate.next()) {
				bDuplicate = true;
				strDomain = rsDuplicate.getFloat("domain") + "";

				if(oSOrigin instanceof TExtTemplate && oTOrigin instanceof TExtTemplate) {
					//START edit the template of the origin
					//get the appropriate replacement for the template, usu. a slash is added
					StringBuffer strbSNewTemplate = new StringBuffer();
					StringBuffer strbTNewTemplate = new StringBuffer();
					String strSWhole = cSrcChunk.domain().substring(0, cSrcChunk.domain().indexOf("."));
					String strTWhole = cTarChunk.domain().substring(0, cTarChunk.domain().indexOf("."));

					for(int i = 0; i < ((TExtTemplate)oSOrigin).size() 
						|| i < ((TExtTemplate)oTOrigin).size(); i++) {
						if(i < ((TExtTemplate)oSOrigin).size()) {
							oTmp = ((TExtTemplate)oSOrigin).get(i);
							if(oTmp instanceof TExtChunk) {
//								System.out.println("oTmp: "+oTmp);
								//no need to add the X.Y because it is already in the template
								//where X.Y is the duplicate chunk
								//or X of X.Y
								if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewTemplate.append(((TExtChunk)oTmp).toString());
									
								}
								//if there's Z where Z is taken from Z.A and if it's already in db
								//no need to add
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewTemplate.append(((TExtChunk)oTmp).toString());

								}
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewTemplate.append(((TExtChunk)oTmp).toString());

								}
								//Z.A should be replaced with Z/X.Y where X.Y is the duplicate chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewTemplate.append(((TExtChunk)oTmp).toString().replace(cSrcChunk.domain(),
										cSrcChunk.domain()+"/"+strDomain));

								}
								//if there's Z where Z is taken from Z.A then no 
								//need to put Z.A instead put the prev chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
										
									strbSNewTemplate.append(((TExtChunk)oTmp).toString().replace(strSWhole,
										strSWhole+"/"+strDomain));
//									strbSNewTemplate.append(((TExtChunk)oTmp).toString());

								}else {
									strbSNewTemplate.append(oTmp);

								}
							}else {
								strbSNewTemplate.append(oTmp);
							}
						}
						
						if(i < ((TExtTemplate)oTOrigin).size()) {
							oTmp = ((TExtTemplate)oTOrigin).get(i);
							if(oTmp instanceof TExtChunk) {
								//no need to add the X.Y because it is already in the template
								//where X.Y is the duplicate chunk
								if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewTemplate.append(((TExtChunk)oTmp).toString());
								}else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewTemplate.append(((TExtChunk)oTmp).toString());
								}else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewTemplate.append(((TExtChunk)oTmp).toString());
								}
								//Z should be replaced with Z/X.Y where X.Y is the duplicate chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewTemplate.append(((TExtChunk)oTmp).toString().replace(cSrcChunk.domain(),
										cSrcChunk.domain()+"/"+strDomain));
								}
								//if there's Z where Z is taken from Z.A then no 
								//need to put Z.A instead put the prev chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									strbTNewTemplate.append(((TExtChunk)oTmp).toString().replace(strTWhole,
										strTWhole+"/"+strDomain));
//									strbTNewTemplate.append(((TExtChunk)oTmp).toString());
								}else {
									strbTNewTemplate.append(oTmp);
								}
							}else {
								strbTNewTemplate.append(oTmp);
							}
						}
						
						if(i < ((TExtTemplate)oSOrigin).size() - 1) {
							strbSNewTemplate.append(" ");
						}
						
						if(i < ((TExtTemplate)oTOrigin).size() - 1) {
							strbTNewTemplate.append(" ");
						}
					}
					//END edit the template of the origin

					//update the template of the origin
					if (strbSNewTemplate.toString().equals("") ||strbSNewTemplate.length()==0 ){
						String strOriginS =  ((TExtTemplate)oSOrigin).toString();
						String strOriginT =  ((TExtTemplate)oTOrigin).toString();
						modifyParentDomainTR(oSOrigin, oTOrigin, cSrcChunk.domain(), strDomain );
						bCheck = true;

					}else{
						String strOriginS =  ((TExtTemplate)oSOrigin).toString();
						String strOriginT =  ((TExtTemplate)oTOrigin).toString();
						modifyParentDomainTR(oSOrigin, oTOrigin, cSrcChunk.domain(), strDomain );
						bCheck = true;
						/*
						bCheck = DBConnector.update("UPDATE Templates SET "
								+ "template" + strSource + " = '" + strbSNewTemplate.toString().replace("'","''") + "' , "
								+ "template" + strTarget + " = '" + strbTNewTemplate.toString().replace("'","''") + "' "
								+ "WHERE template" + strSource + " = '"
								+ ((TExtTemplate)oSOrigin).toString() + "' AND "
								+ "template" + strTarget + " = '"
								+ ((TExtTemplate)oTOrigin).toString() + "' AND "
								+ "compID" + strSource + " = '"
								+ ((TExtTemplate)oSOrigin).getCompoundIdentifier() + "' AND "
								+ "compID" + strTarget + " = '"
								+ ((TExtTemplate)oTOrigin).getCompoundIdentifier() + "'");		
						*/
						
						TExtTemplate tSource = new TExtTemplate(strbSNewTemplate.toString().trim(),((TExtTemplate)oSOrigin).getCompoundIdentifier() );
						TExtTemplate tTarget = new TExtTemplate(strbTNewTemplate.toString().trim(),((TExtTemplate)oTOrigin).getCompoundIdentifier() );
						
						
						((TExtTemplate)oSOrigin).clear();
						((TExtTemplate)oTOrigin).clear();
						
						((TExtTemplate)oSOrigin).addAll(tSource);
						((TExtTemplate)oTOrigin).addAll(tTarget);
						
					}
					

					
					TExtTemplate tNewSource = new TExtTemplate(strbSNewTemplate.toString().trim(),
								((TExtTemplate)oSOrigin).getCompoundIdentifier());
					TExtTemplate tNewTarget = new TExtTemplate(strbTNewTemplate.toString().trim(),
								((TExtTemplate)oTOrigin).getCompoundIdentifier());
		
					AlignedTemplate atNewRefinement = new AlignedTemplate(tNewSource, tNewTarget);
					if(vTRTemplates.size() > 0) {
						vTRTemplates.removeElementAt(vTRTemplates.size()-1);
					}
					vTRTemplates.add(atNewRefinement);
		
					//START adding blanks - ensures no multiple blanks
					StringBuffer strbChunkDom = new StringBuffer(cSrcChunk.domain());
					boolean bBlankDuplicate = false;
					boolean bWholeDomExists = false;
					
					if(strbChunkDom.toString().contains(".")) {
						strbChunkDom = new StringBuffer(strbChunkDom.substring(0, strbChunkDom.indexOf(".")));
					}
					
					rsDuplicateBlank = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
								+ "chunk" + strSource + " = ' '"
								+ " AND chunk" + strTarget + " = ' '"
								+ " AND domain LIKE '" + strbChunkDom.toString() + ".%'"
								+ " AND compID" + strSource + " = 'none'"
								+ " AND compID" + strTarget + " = 'none'");
				
//					System.out.println("strbChunkDom: "+strbChunkDom.toString());
					
					try {
						//check for duplicate blank with the same domain
						while(rsDuplicateBlank.next()) {
							bBlankDuplicate = true;
//							System.out.println("----->>>duplicate blank!!!");
						}
					} catch(SQLException e){
						e.printStackTrace();
						//System.out.println("There was an error while updating the chunks from refinement in the database.");
						bCheck = false;
					}
					
					ResultSet rsWholeDomExists = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
								+ "domain LIKE '" + strbChunkDom.toString() + ".%'");
					
//					System.out.println("strbChunkDom: "+strbChunkDom.toString());
					
					try {
						//check for duplicate blank with the same domain
						while(rsWholeDomExists.next()) {
							bWholeDomExists = true;
//							System.out.println("----->>>WholeDomExists!!!");
						}
					} catch(SQLException e){
						e.printStackTrace();
						//System.out.println("There was an error while updating the chunks from refinement in the database.");
						bCheck = false;
					}
					
					if(!bBlankDuplicate && !bWholeDomExists) {
						//v added.. check din ung baba. ganito rin ginawa ko.
						String strCS = oSOrigin.toString();
						String strCT = oTOrigin.toString();
						modifyParentDomainTR(oSOrigin,oTOrigin,cSrcChunk.domain(), strDomain);
	
						bCheck = true;
						/*
						bCheck = DBConnector.update("INSERT INTO Chunks"
								+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
								+ "constCnt" + strSource.substring(0, 3) + ", constCnt" + strTarget.substring(0, 3)
								+ ") VALUES (' ', ' ', '" 
								+ cSrcChunk.domain()
								+ "', "
								+ "0, 0)");
						*/
						
					}
					//END adding blanks - ensures no multiple blanks
					strbSNewTemplate = null;
					strbTNewTemplate = null;
					strSWhole = null;
					strTWhole = null;
					strbChunkDom = null;
				}
				//assumes: that oSOrigin and oTOrigin is a TExtChunk
				else {
					//START edit the chunk of the origin
					//get the appropriate replacement for the chunk, usu. a slash is added
					StringBuffer strbSNewChunk = new StringBuffer();
					StringBuffer strbTNewChunk = new StringBuffer();
					String strSWhole = cSrcChunk.domain().substring(0, cSrcChunk.domain().indexOf("."));
					String strTWhole = cTarChunk.domain().substring(0, cTarChunk.domain().indexOf("."));

					for(int i = 0; i < ((TExtChunk)oSOrigin).size() 
						|| i < ((TExtChunk)oTOrigin).size(); i++) {
						if(i < ((TExtChunk)oSOrigin).size()) {
							oTmp = ((TExtChunk)oSOrigin).get(i);
							if(oTmp instanceof TExtChunk) {
								//no need to add the X.Y because it is already in the chunk
								//where X.Y is the duplicate chunk
								if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewChunk.append(((TExtChunk)oTmp).toString());
								}
								//no need to add
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewChunk.append(((TExtChunk)oTmp).toString());
								}
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewChunk.append(((TExtChunk)oTmp).toString());
								}
								//Z should be replaced with Z/X.Y where X.Y is the duplicate chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewChunk.append(((TExtChunk)oTmp).toString().replace(cSrcChunk.domain(),
										cSrcChunk.domain()+"/"+strDomain));
								}
								//if there's Z where Z is taken from Z.A then no 
								//need to put Z.A instead put the prev chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
										strbSNewChunk.append(((TExtChunk)oTmp).toString().replace(strSWhole,
										strSWhole+"/"+strDomain));
//									strbSNewChunk.append(((TExtChunk)oTmp).toString());
								}else {
									strbSNewChunk.append(oTmp);
								}
							}else {
								strbSNewChunk.append(oTmp);
							}
						}
						
						if(i < ((TExtChunk)oTOrigin).size()) {
							oTmp = ((TExtChunk)oTOrigin).get(i);
							if(oTmp instanceof TExtChunk) {
								//no need to add the X.Y because it is already in the chunk
								//where X.Y is the duplicate chunk
								if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewChunk.append(((TExtChunk)oTmp).toString());
								}else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewChunk.append(((TExtChunk)oTmp).toString());
								}else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewChunk.append(((TExtChunk)oTmp).toString());
								}
								//Z should be replaced with Z/X.Y where X.Y is the duplicate chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewChunk.append(((TExtChunk)oTmp).toString().replace(cSrcChunk.domain(),
										cSrcChunk.domain()+"/"+strDomain));
								}
								//if there's Z where Z is taken from Z.A then no 
								//need to put Z.A instead put the prev chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
										strbTNewChunk.append(((TExtChunk)oTmp).toString().replace(strTWhole,
										strTWhole+"/"+strDomain));
//									strbTNewChunk.append(((TExtChunk)oTmp).toString());
								}else {
									strbTNewChunk.append(oTmp);
								}
							}else {
								strbTNewChunk.append(oTmp);
							}
						}
						
						if(i < ((TExtChunk)oSOrigin).size() - 1) {
							strbSNewChunk.append(" ");
						}
						
						if(i < ((TExtChunk)oTOrigin).size() - 1) {
							strbTNewChunk.append(" ");
						}
					}
					//END edit the template of the origin
					
						
						String strOriginS =  ((TExtChunk)oSOrigin).toString();
						String strOriginT =  ((TExtChunk)oTOrigin).toString();
						// my [5/3.2/3.1] should be replaced with my [3.3/3.2/3.1]
						// note that 3.3 has been found and in strDomain
						modifyParentDomainTR(oSOrigin, oTOrigin, cSrcChunk.domain(), strDomain );
						bCheck = true;			
						
						/*
						bCheck = DBConnector.update("UPDATE Chunks SET "
								+ "chunk" + strSource + " = '" + strbSNewChunk.toString().replace("'","''") + "' , "
								+ "chunk" + strTarget + " = '" + strbTNewChunk.toString().replace("'","''") + "' "
								+ "WHERE chunk" + strSource + " = '" 
								+ ((TExtChunk)oSOrigin).toString() + "' AND "
								+ "chunk" + strTarget + " = '"
								+ ((TExtChunk)oTOrigin).toString() + "' AND "
								+ "compID" + strSource + " = '"
								+ ((TExtChunk)oSOrigin).getCompoundIdentifier() + "' AND "
								+ "compID" + strTarget + " = '"
								+ ((TExtChunk)oTOrigin).getCompoundIdentifier() + "'");						
						*/
					
					
					//update the chunk of the origin

					
					//START adding blanks - ensures no multiple blanks
					StringBuffer strbChunkDom = new StringBuffer(cSrcChunk.domain());
					boolean bBlankDuplicate = false;
					boolean bWholeDomExists = false;
					
					if(strbChunkDom.toString().contains(".")) {
						strbChunkDom = new StringBuffer(strbChunkDom.substring(0, strbChunkDom.indexOf(".")));
					}
					
					rsDuplicateBlank = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
								+ "chunk" + strSource + " = ' '"
								+ " AND chunk" + strTarget + " = ' '"
								+ " AND domain LIKE '" + strbChunkDom.toString() + ".%'"
								+ " AND compID" + strSource + " = 'none'"
								+ " AND compID" + strTarget + " = 'none'");
				
//					System.out.println("strbChunkDom: "+strbChunkDom.toString());
					
					try {
						//check for duplicate blank with the same domain
						while(rsDuplicateBlank.next()) {
							bBlankDuplicate = true;
//							System.out.println("----->>>duplicate blank!!!");
						}
					} catch(SQLException e){
						e.printStackTrace();
						//System.out.println("There was an error while updating the chunks from refinement in the database.");
						bCheck = false;
					}
					
					ResultSet rsWholeDomExists = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
								+"domain LIKE '" + strbChunkDom.toString() + ".%'");
				
					
					try {
						//check for duplicate blank with the same domain
						while(rsWholeDomExists.next()) {
							bWholeDomExists = true;
						}
					} catch(SQLException e){
						e.printStackTrace();
						//System.out.println("There was an error while updating the chunks from refinement in the database.");
						bCheck = false;
					}
					
					if(!bBlankDuplicate && !bWholeDomExists) {

						
						String strCS = oSOrigin.toString();
						String strCT = oTOrigin.toString();
						modifyParentDomainTR(oSOrigin,oTOrigin,cSrcChunk.domain(), strDomain);
						bCheck = true;
						
						/*
						bCheck = DBConnector.update("INSERT INTO Chunks"
								+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
								+ "constCnt" + strSource.substring(0, 3) + ", constCnt" + strTarget.substring(0, 3)
								+ ") VALUES (' ', ' ', '" 
								+ cSrcChunk.domain()
								+ "', "
								+ "0, 0)");
						*/
					}
					//END adding blanks - ensures no multiple blanks
					strbSNewChunk = null;
					strbTNewChunk = null;
					strSWhole = null;
					strTWhole = null;
					strbChunkDom = null;
				}
				strDomain = null;
			}
		} catch(SQLException sqle){
			sqle.printStackTrace();
			bCheck = false;
		}
		
//		System.out.println("bCheck: "+bCheck);
//		System.out.println("bDuplicate: "+bDuplicate);
//		System.out.println("*****END UPDATE DUPLICATE CHUNKS*****");

		rsDuplicate = null;
		
		return bDuplicate;
	}
	
	
	
	
	/**
	 * Checks if the domain is a part of that domain or is that domain.
	 * example: returns true for 98, 98.3
	 * false for 98, 85.4
	 * true for 95.5, 95.5
	 * 
	 * @param strDomainReference
	 * @param strToBeChecked
	 * @return
	 */
	public boolean domainIsPartOf(String strDomainReference, String strToBeChecked){
		if(strDomainReference.equals(strToBeChecked)) return true;
		
		if(TExtUtility.compareBaseDomain(strDomainReference, strToBeChecked)) return true;
		
		return false;
	}
	
	
	
	
	
	
	
	
	/**
	 *  Vince new code. for modifying parents...
	 *  but this should be done outside the class.
	 *  
	 *  will add the domains in strNewDomain to the appropriate domain set
	 *  in the parent
	 * 
	 */ 
	public void modifyParentDomainTR(Object ctParentS, Object ctParentT, 
		String strOldDomain, String strNewDomain) {
		String strSource = new String();
		String strTarget = new String();
		String strQuery = new String();
		String strTable = "Template";
		
		if(bSourceIsFilipino){
			strSource="Filipino";
			strTarget="English";
		}else{
			strSource="English";
			strTarget="Filipino";
		}
		
		
		if (ctParentS instanceof TExtTemplate && ctParentT instanceof TExtTemplate){
			TExtTemplate tSourceRefinement = (TExtTemplate)ctParentS;
			TExtTemplate tTargetRefinement = (TExtTemplate)ctParentT;
			strTable = "Template";
			strQuery = "SELECT templateID FROM Templates WHERE "
				+ "template" + strSource + " = '" 
				+ tSourceRefinement.toString().replace("'","''") + "'"
				+ " AND template" + strTarget + " = '" 
				+ tTargetRefinement.toString().replace("'","''") + "'"
				+ " AND compID" + strSource + " = '" 
				+ tSourceRefinement.getCompoundIdentifier() + "'"
				+ " AND compID" + strTarget + " = '" 
				+ tTargetRefinement.getCompoundIdentifier() + "'";
		}
		  

			  
		if (ctParentS instanceof TExtChunk && ctParentT instanceof TExtChunk){
			TExtChunk cSourceRefinement = (TExtChunk)ctParentS;
			TExtChunk cTargetRefinement = (TExtChunk)ctParentT;
			strTable = "Chunk";
			strQuery = "SELECT chunkID FROM Chunks WHERE "
				+ "chunk" + strSource + " = '" 
				+ cSourceRefinement.toString().replace("'","''") + "'"
				+ " AND domain LIKE '"+ cSourceRefinement.domain()+"' "
				+ " AND chunk" + strTarget + " = '" 
				+ cTargetRefinement.toString().replace("'","''") + "'"
				+ " AND compID" + strSource + " = '" 
				+ cSourceRefinement.getCompoundIdentifier() + "'"
				+ " AND compID" + strTarget + " = '" 
				+ cTargetRefinement.getCompoundIdentifier() + "'";
		}

		
		
		ResultSet rs;
		
		rs = DBConnector.select(strQuery);
		
			boolean x = true;
			String strTempS = new String();
			String strTempT = new String();
			String strID = new String();
			
			strTempS = ctParentS.toString(); //chunkEnglish or templateEnglish
			strTempT = ctParentT.toString();
			
			try{
				if (rs.next()) {
					strID = rs.getString(strTable+"ID");
					strTempS = ctParentS.toString(); //chunkEnglish or templateEnglish
					strTempT = ctParentT.toString();
				}
				rs.close();
			}catch(SQLException s){
				System.out.println("EXCEPTION IN MODPARENT DOMAIN");
				s.printStackTrace();
			}
			
			strTempS = strTempS.replace("["+strOldDomain+"]" , "["+strNewDomain+"]");
			strTempT = strTempT.replace("["+strOldDomain+"]" , "["+strNewDomain+"]");

			for(Iterator it = ((Vector)ctParentT).iterator(); it.hasNext();  ){
				Object o =  it.next();
				if (o instanceof TExtChunk) {
					String strCTDom = ((TExtChunk)o).domain(); //[3] [3/4.2]
					strCTDom = stripSquareBrackets(strCTDom);
					//to see if [3.1] belongs to [3]
					if (domainIsPartOf(strCTDom,strOldDomain ) ){
						strTempS = strTempS.replace("["+strCTDom+"]" , "["+strCTDom+"/"+strNewDomain+"]");
						strTempT = strTempT.replace("["+strCTDom+"]" , "["+strCTDom+"/"+strNewDomain+"]");
					}
				}
			}
			/*
			 * generalization is not done in TR chunkRefinement. it should be done in the updateDB
			 * but i can remove duplicates here so... i will
			strTempS = generalizeDomains(strTempS);
			strTempT = generalizeDomains(strTempT);
			*/
			
			strTempS = TExtLearnerUtility.removeDuplicateDomains(strTempS);
			strTempT = TExtLearnerUtility.removeDuplicateDomains(strTempT);
			
			//chunk +s = "chunks" = the table
			
			if (strTempT.equals("")) return;
				
				
			String strUpdate = "UPDATE "+ strTable +"s SET " 
					+ strTable+strSource + " ='"
					+ strTempS.trim().replace("'","''") + "', "
					+ strTable+strTarget +  "= '" 
					+ strTempT.trim().replace("'","''") + "' "
					+ "WHERE "+ strTable + "ID = '"+ strID + "'";
				
			
			if (!strTempS.trim().equals(ctParentS))
			DBConnector.update(strUpdate);
			
			if(ctParentS instanceof TExtTemplate){
				TExtTemplate tSource = new TExtTemplate(strTempS,((TExtTemplate)ctParentS).getCompoundIdentifier() );
				TExtTemplate tTarget = new TExtTemplate(strTempT,((TExtTemplate)ctParentT).getCompoundIdentifier() );
				
				
				((TExtTemplate)ctParentS).clear();
				((TExtTemplate)ctParentT).clear();
				
				((TExtTemplate)ctParentS).addAll(tSource);
				((TExtTemplate)ctParentT).addAll(tTarget);
			}
			
			if(ctParentS instanceof TExtChunk){
				TExtChunk cSource = new TExtChunk(strTempS, ((TExtChunk)ctParentS).domain() );
				TExtChunk cTarget = new TExtChunk(strTempT, ((TExtChunk)ctParentT).domain() );
				
				cSource.setCompoundIdentifier( ((TExtChunk)ctParentS).getCompoundIdentifier());
				cSource.adjustChunkCompound();
				cTarget.setCompoundIdentifier( ((TExtChunk)ctParentT).getCompoundIdentifier());
				cTarget.adjustChunkCompound();
				
				((TExtChunk)ctParentS).clear();
				((TExtChunk)ctParentT).clear();
				
				((TExtChunk)ctParentS).addAll(cSource);
				((TExtChunk)ctParentT).addAll(cTarget);
				
			}
	
	}
	

	
	
	
	
	
	
	public void modifyParentDomain(Object ctParentS, Object ctParentT, 
			String strOldDomain, String strNewDomain) {
			String strSource = new String();
			String strTarget = new String();
			String strQuery = new String();
			String strTable = "Template";
			
			if(bSourceIsFilipino){
				strSource="Filipino";
				strTarget="English";
			}else{
				strSource="English";
				strTarget="Filipino";
			}
			
			
			if (ctParentS instanceof TExtTemplate && ctParentT instanceof TExtTemplate){
				TExtTemplate tSourceRefinement = (TExtTemplate)ctParentS;
				TExtTemplate tTargetRefinement = (TExtTemplate)ctParentT;
				strTable = "Template";
				strQuery = "SELECT templateID FROM Templates WHERE "
					+ "template" + strSource + " = '" 
					+ tSourceRefinement.toString().replace("'","''") + "'"
					+ " AND template" + strTarget + " = '" 
					+ tTargetRefinement.toString().replace("'","''") + "'"
					+ " AND compID" + strSource + " = '" 
					+ tSourceRefinement.getCompoundIdentifier() + "'"
					+ " AND compID" + strTarget + " = '" 
					+ tTargetRefinement.getCompoundIdentifier() + "'";
			}
			  

				  
			if (ctParentS instanceof TExtChunk && ctParentT instanceof TExtChunk){
				TExtChunk cSourceRefinement = (TExtChunk)ctParentS;
				TExtChunk cTargetRefinement = (TExtChunk)ctParentT;
				strTable = "Chunk";
				strQuery = "SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = '" 
					+ cSourceRefinement.toString().replace("'","''") + "'"
					+ " AND domain LIKE '"+ cSourceRefinement.domain()+"' "
					+ " AND chunk" + strTarget + " = '" 
					+ cTargetRefinement.toString().replace("'","''") + "'"
					+ " AND compID" + strSource + " = '" 
					+ cSourceRefinement.getCompoundIdentifier() + "'"
					+ " AND compID" + strTarget + " = '" 
					+ cTargetRefinement.getCompoundIdentifier() + "'";
			}

			
			
			ResultSet rs;
			
			//for templates
			rs = DBConnector.select(strQuery);
			

				String strTempS = new String();
				String strTempT = new String();
				String strID = new String();
				
				strTempS = ctParentS.toString(); //chunkEnglish or templateEnglish
				strTempT = ctParentT.toString();
				
				try{
					if (rs.next()) {
						strID = rs.getString(strTable+"ID");
						strTempS = ctParentS.toString(); //chunkEnglish or templateEnglish
						strTempT = ctParentT.toString();
					}
					rs.close();
				}catch(SQLException s){
					System.out.println("EXCEPTION IN MODPARENT DOMAIN");
					s.printStackTrace();
				}
				
				strTempS = strTempS.replace("["+strOldDomain+"]" , "["+strNewDomain+"]");
				strTempT = strTempT.replace("["+strOldDomain+"]" , "["+strNewDomain+"]");

				for(Iterator it = ((Vector)ctParentT).iterator(); it.hasNext();  ){
					Object o =  it.next();
					if (o instanceof TExtChunk) {
						String strCTDom = ((TExtChunk)o).domain(); //[3] [3/4.2]
						strCTDom = stripSquareBrackets(strCTDom);
						//to see if [3.1] belongs to [3]
						if (domainIsPartOf(strCTDom,strOldDomain ) ){
							strTempS = strTempS.replace("["+strCTDom+"]" , "["+strCTDom+"/"+strNewDomain+"]");
							strTempT = strTempT.replace("["+strCTDom+"]" , "["+strCTDom+"/"+strNewDomain+"]");
						}
					}
				}
				
				strTempS = tlu.generalizeDomains(strTempS);
				strTempT = tlu.generalizeDomains(strTempT);
				
				//end v
					
				//always [91/35.1] [92] is already in DB! 
				//how do i modify always [91] [92].. sino nagmodify nito to that
				
				//chunk +s = "chunks" = the table
				
				if (strTempT.equals("")) return;
					
					
				String strUpdate = "UPDATE "+ strTable +"s SET " 
						+ strTable+strSource + " ='"
						+ strTempS.trim().replace("'","''") + "', "
						+ strTable+strTarget +  "= '" 
						+ strTempT.trim().replace("'","''") + "' "
						+ "WHERE "+ strTable + "ID = '"+ strID + "'";
					
				
				
				DBConnector.update(strUpdate);
				
				if(ctParentS instanceof TExtTemplate){
					TExtTemplate tSource = new TExtTemplate(strTempS,((TExtTemplate)ctParentS).getCompoundIdentifier() );
					TExtTemplate tTarget = new TExtTemplate(strTempT,((TExtTemplate)ctParentT).getCompoundIdentifier() );
					
					
					((TExtTemplate)ctParentS).clear();
					((TExtTemplate)ctParentT).clear();
					
					((TExtTemplate)ctParentS).addAll(tSource);
					((TExtTemplate)ctParentT).addAll(tTarget);
				}
				
				if(ctParentS instanceof TExtChunk){
					TExtChunk cSource = new TExtChunk(strTempS, ((TExtChunk)ctParentS).domain() );
					TExtChunk cTarget = new TExtChunk(strTempT, ((TExtChunk)ctParentT).domain() );
					
					cSource.setCompoundIdentifier( ((TExtChunk)ctParentS).getCompoundIdentifier());
					cSource.adjustChunkCompound();
					cTarget.setCompoundIdentifier( ((TExtChunk)ctParentT).getCompoundIdentifier());
					cTarget.adjustChunkCompound();
					
					((TExtChunk)ctParentS).clear();
					((TExtChunk)ctParentT).clear();
					
					((TExtChunk)ctParentS).addAll(cSource);
					((TExtChunk)ctParentT).addAll(cTarget);
					
				}
		
		}
	
	
	
	
	
	
	
	
	
	
	
	
//MIN END
}